6
votes

I have a Windows Store application that uses a background task. The background task is stored in a Windows Runtime Component project. (This structure appears to be the only way to make background tasks work.)

In the background task project, I have some externally visible public methods that have return/parameter types that are my own classes rather than Windows Runtime classes.

For example:

public MyClass DoSomething()
{
    return null;
}

When I build, I receive errors such as the following in relation to these methods:

Method 'X' returns 'Y', which is not a valid Windows Runtime type. Methods exposed to Windows Runtime must return only Windows Runtime types.

and

Method 'T' has parameter 'U' of type 'W'. 'W' is not a valid Windows Runtime parameter type.

I can understand what the errors are saying, but I haven't thought of a good way I can structure my code so that I meet these requirements.

Here are some things I have already considered:

  1. Changing the background task project to a Windows Store Class Library project. This allowed the use of the non-Windows Runtime types in the method signatures, but the background task no longer launched.
  2. Using a portable class library. This didn't work because it didn't have access to the Windows Runtime.
  3. For value types, I can break them up into Tuples or multiple parameters, but this seems messy, less structured and less maintainable. I am strongly against this type of programming.
  4. For classes, it seems that I might have to duplicate their logic across both applications. This is a huge maintainability problem.
1
You are making this needlessly hard to answer by not showing us what these methods look like. Custom types require building a proxy, check this answer.Hans Passant
@HansPassant: Thanks! I've edited the question to make it a bit clearer.Sam
I still can't see what these methods look like.Hans Passant
@HansPassant, I've added a simple example for you.Sam

1 Answers

6
votes

I was able to make this work by isolating the background task in a Windows Component project and moving the re-usable Windows Runtime-dependent code into a Windows Store Class Library project.