locked
Universal Class Library for Shared Code Between Windows Store 8.1 & Windows Phone 8.1 Apps

    Question

  • I have two Universal Apps (A shared project, Windows Phone and Windows Store projects) and I want to create a class library containing some shared code.

    I would like to use the #if #else #endif pre-processor directives in my class library, just like I do in my shared projects. There doesn't seem to be a project template in the 'New Project' dialogue for creating a Shared project though.

    If I create a normal class library, I would have to create one for Windows Phone and one for Windows store and link files between them. How can I create a Universal Shared Class Library?


    Blacklight

    Monday, May 12, 2014 12:03 PM

Answers

All replies

  • Under Universal Apps there is a project type "Class Library (Portable for Universal Apps)"

    Tuesday, May 13, 2014 1:43 AM
    Owner
  • I looked at this project type. It's just a standard portable class library targeted at Windows 8.1 and Windows Phone 8.1. The problem is, that I can't use #if #else #endif pre-processor directives in this project type.

    If I want to use #if #else #endif pre-processor directives, do I have to create two projects and use linked files instead? There does not seem to be an option to create the 'Shared' project type with the '<>' icon. That project type seems to be only created when you create a universal app.


    Blacklight

    Wednesday, May 14, 2014 9:05 AM
  • what you ask is not possible a PCL compiles into a dll and that dll can be linked to a phone or windows project; so at the moment of compiling the code there is knowlegde if its windows or windows phone.

    can't use use techniques as Dependency injection to inject the platform specific code in your library? or make extends from your base classes in your library?


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Wednesday, May 14, 2014 9:09 AM
  • If you have the same code on both platforms a portable class library is the recommended approach. However, if you have a small amount of platform specific code in your class I thought that with the advent of the Shared project, this was now a solved problem.

    It seems then that for writing cross platform class libraries you still need separate projects and then use either interfaces as you suggest or file linking.

    The new shared project seems to consist of just a project file and a modification of the app projects to include the shared project file. If I copied this approach manually to create more than one shared project and then link these into the app projects that should surely work.

    My only hesitation is that Microsoft did not include this functionality out of the box and they must have done so for a reason.


    Blacklight

    Wednesday, May 14, 2014 9:22 AM
  • you can try to put everything in a WinRT component. then you get 3 new projects (windows, winphone ans shared project) and this compiles to seperate dll's again.

    also have a look at this extension: http://visualstudiogallery.msdn.microsoft.com/315c13a7-2787-4f57-bdf7-adae6ed54450


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Wednesday, May 14, 2014 9:28 AM
  • Excellent, I guess Microsoft wanted to include this feature, they just didn't have the the time to include it into Visual Studio 2013 Update 2.

    One thing to note about this is that shared projects cannot reference other shared projects. Also, this is preview technology.


    Blacklight


    Wednesday, May 14, 2014 9:33 AM