简体   繁体   中英

How can I share code between a Shared Universal project and a Background Task?

Right now I have a Universal Project which contains mostly business logic and models. These I can access from both the Windows Phone project AND the Windows project.

But when I add a Background Task, I would like to access that shared logic as well, because otherwise I'd have to duplicate most of my code in my background task.

I know you can do "Add as Link", but this causes compilation errors like this:

Method 'MyProject.Model.Playlist.Tracks.get()' has a parameter of type 'System.Collections.ObjectModel.ObservableCollection' in its signature. Although this generic type is not a valid Windows Runtime type, the type or its generic parameters implement interfaces that are valid Windows Runtime types. Consider changing the type 'System.Collections.ObjectModel.ObservableCollection' in the method signature to one of the following types instead: 'System.Collections.Generic.IList, System.Collections.Generic.IReadOnlyList, System.Collections.Generic.IEnumerable'.

These can be worked away, by either using the internal keyword on functions / classes where needed, or converting my code to the suggested types.

Unfortunately, I don't think this is possible in my case, because SQLiteNet-Extensions uses the properties to determine its relations, and only certain types are supported ( List , array , and I added ObservableCollection ).

So I'm looking for a way to share code between my Shared project and my BackgroundTask, without getting into the "valid Windows Runtime type" errors.

You can share code between Windows Phone and Windows 8 app projects by creating a Portable Class Library . You then need to add a reference to the library in each of the projects which should use it.

Unfortunately you will be constrained to the common denominators across the target frameworks. MSDN details these. Available members for PCL and your target frameworks will appear in Intellisense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM