1

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,

  1. 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

  2. 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

3
  • Is your UWP Class Library a .NET 5 library? .NET Standard? Because it should work with .NET Standard, but may not be compatible yet with .NET 5. Commented Aug 17, 2021 at 21:38
  • @TamBui how can I check it? Commented Aug 17, 2021 at 21:42
  • My bad, I was going down the wrong tangent. Read my answer instead. Commented Aug 17, 2021 at 21:49

2 Answers 2

1

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> 
Sign up to request clarification or add additional context in comments.

2 Comments

A WPF application can reference a UWP class library, and can even host UWP controls within the WPF app. Take a look at 'XAML Islands' and you'll see that it is possible. Of course, all of this should be increasingly moot once WinUI 3 comes out.
I know it's possible to host UWP controls in a WPF app but you should still not reference a UWP class library from a desktop app.
1

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> 

4 Comments

Please see my updated post. There is no compiler errors anymore after following your suggestion but now getting build error.
Those are warnings, not errors. To get rid of the first warning, edit the first line in your project to say <Project Sdk="Microsoft.NET.Sdk". To get rid of the second warning, you need to match your WPF project's platform architecture to your UWP project's architecture. To do that, right-click your Solution in Solution Explorer and choose "Configuration Manager..."
anyway the build error is frustrating me. Anyway to know where is the problem?
What is the build error you're getting? In your previous comment, you stated "There is no compiler errors anymore after following your suggestion". Then, you posted two warnings. I don't see you posted any build error, unless I'm missing it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.