How to link UWP Class Library into an WPF Application?
I have created an UWP Class Library with a single test class. I have an WPF .NET application which wants to consume that class library. What are the steps I need to follow?
Following this tutorial, I wanted to add library MyLib
in my Application MyApp
. But I am finding following compiler errors,
Severity Code Description Project File Line Suppression State Error NU1201 Project MyLib is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Project MyLib supports: uap10.0.19041 (UAP,Version=v10.0.19041) MyApp C:\Users....\MyApp.csproj
##UPDATE
After adding following code,
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<AssetTargetFallback>uap10.0.19041</AssetTargetFallback>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MyLib\MyLib.csproj" />
</ItemGroup>
</Project>
as suggested by TamBui in the answer, I am getting build error. However there have been two compiler warnings from the beginning. Sharing if it can give any clue,
Warning NETSDK1137 It is no longer necessary to use the Microsoft.NET.Sdk.WindowsDesktop SDK. Consider changing the Sdk attribute of the root Project element to 'Microsoft.NET.Sdk'. MyApp C:\Program Files\dotnet\sdk\5.0.103\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets 376
Warning MSB3270 There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "C:\Users...\MyLib\bin\x64\Debug\MyLib.dll", "AMD64". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. MyApp C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets 2123
You need to add the AssetTargetFallback
that matches your UWP project's target version to your WPF project's PropertyGroup
. Select your WPF project in the Solution Explorer, and you will be able to edit the project's properties.
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_68824006</RootNamespace>
<AssetTargetFallback>uap10.0.19041</AssetTargetFallback>
<UseWPF>true</UseWPF>
</PropertyGroup>
A WPF application cannot reference a UWP class library. In short, the two different platforms or frameworks are not compatible with each other.
You should change the target framework of your class library (MyLib.csproj
) to either .NET Standard or the same framework that your WPF app targets:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>