.netwpfuwpcode-sharing

Sharing C# code among WPF, UWP, Phone 81 and Compact Framework


For years we've shared our C# code among these platforms by creating projects for each platform inside a directory containing all the source code for that project. As we added new platforms, if a code file containing wouldn't compile on the new platform we would create a copy of the code file and modify it so that it would work on it's intended platform.

A project directory might look like this

Application.Base
    Applications.Base.CF.csproj
    Applications.Base.WPF.csproj
    Applications.Base.Phone81.csproj
    Properties
        AssemblyInfo.cs
    Src
        WidgetWPF.cs
        WidgetPhone81.cs
        Widget.cs

Now I'm taking the same approach for UWP and I'm running into this issue. When adding a new project for UWP:

Application.Base
    ....
    Applications.Base.UWP
    Properties
        AssemblyInfo.cs
        DSI.Applications.Base.UWP.rd.xml
    ...

Now when I go to my WPF solution, I get an error compiling the Applications.Base.WPF project (event though no UWP project files are included in the WPF project):

Your project is not referencing the ".NETFramework,Version=v4.6.1" framework. Add a reference to ".NETFramework,Version=v4.6.1" in the "frameworks" section of your project.json, and then re-run NuGet restore. DSI.Applications.Base

After some A/B testing, I find that I can fix the error by using nuget to remove the UWP's reference to Microsoft.NETCore.UniversalWindowsPlatform. Of course that breaks the UWP project. There is no project.json present at all.

Is there a workaround that will allow us to continue code sharing in this manner? Refactoring our shared projects to accommodate all platforms would be a huge burden - there are many shared projects like this - some of which are shared to even additional platforms (our ASP.Net project.)


Solution

  • In our case, the intermediate obj folder was the culprit. Building our WPF project failed only if artifacts of the UWP build were left in the obj folder. The error did lead me to the issue but not directly. There was no project.json file in the project directory as the error indicated, but in the obj folder there was a project.assets.json file. I removed all of the obj contents just before a new WPF build and it succeeded.

    In order to accommodate our shared directory, I changed intermediate (and bin) directory of the UWP build to objUWP by modifying the project file. I didn't find a way to do it directly in VS 2017, so I modified the project xml directly:

      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <PlatformTarget>AnyCPU</PlatformTarget>
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>binUWP\Debug\</OutputPath>
        <BaseIntermediateOutputPath>objUWP\</BaseIntermediateOutputPath>
        <DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>