Wpf: WPF support to .vcxproj

Created on 25 Oct 2019  路  16Comments  路  Source: dotnet/wpf

I would like to recompile (https://github.com/microsoft/WPFDXInterop) to support .net core 3.
I couldn't find any reference or sample explaing how to add WPF support to .vcxproj
I spent half a day looking everywhere with no luck.
I tryed:

<CLRSupport>NetCore</CLRSupport>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
issue-type-question issue-type-tracking-external

Most helpful comment

Try this:

  <ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" />
  </ItemGroup>

/cc @nguerrera

All 16 comments

Try this:

  <ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" />
  </ItemGroup>

/cc @nguerrera

We have the same problem.

The question is, how to set the SDK in the .vcxproj .

In .csproj you would simply use this as the first line:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
But that does not work in .vcxproj.
Without that line we get this error message:
C:\Program Files\dotnet\sdk\3.1.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(349,5): warning NETSDK1107: Microsoft.NET.Sdk.WindowsDesktop is required to build Windows desktop applications. 'UseWpf' and 'UseWindowsForms' are not supported by the current SDK.

It is not supported to use the windows desktop sdk in vcxproj at this time. You can add the framework reference as shown above and call WPF API from a C++ library. However, you can鈥檛 currently have xaml, etc. in C++ Core project.

It is not supported to use the windows desktop sdk in vcxproj at this time. You can add the framework reference as shown above and call WPF API from a C++ library. However, you can鈥檛 currently have xaml, etc. in C++ Core project.

Thanks, I know that .xaml is not supported in .net core c++, and we do not need that.
However, what we need is the possibility to call code from the PresentationCore or WindowsBase assembly , e.g. access to the WPF Dispatcher class for threaded interoperability in our class library.

It actually works to add references to the .vcxproj like this:


C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.1.0\ref\netcoreapp3.1\PresentationCore.dll


C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.1.0\ref\netcoreapp3.1\PresentationFramework.dll

However, it fails for WindowsBase.dll

<Reference Include="WindowsBase">
  <HintPath>C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.1.0\ref\netcoreapp3.1\WindowsBase.dll</HintPath>
</Reference>

We get errors like this:

test.h(31,2): warning C4691: 'System::Windows::Threading::Dispatcher': type referenced was expected in unreferenced assembly 'WindowsBase', type defined in current translation unit used instead (compiling source file test.cpp)

Did you ever try to call WPF code like System::Windows::Threading::Dispatcher from a .netcore 3.1 c++ class library .dll? Is there a sample available for this?

Never mind. I just noticed that it seems possible to simply let the compile ignore that warning 4691 (I considered that as error because we treat all warnings as errors). So, this might be simply a false positive warning message.
--> It works for us by ignoring the warning in the compiler settings of the project

It actually works to add references to the .vcxproj like this:

You should use FrameworkReference as shown above: https://github.com/dotnet/wpf/issues/2117#issuecomment-546494357

With a bit more detail, you can use any of these:

<ItemGroup>
    <!-- Reference the entirety of the windows desktop framework: winforms, wpf, and the types that provide integration between them -->
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" />

    <!-- Reference all of WPF -->
    <FrameworkReference Include="Microsoft.WindowsDesktop.App.WPF" />

    <!-- Reference all of winforms -->
    <FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" />

    <!-- Reference all of ASP.NET -->
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

The WindowsDesktop SDK will select from above based on the values of UseWpf and UseWindowsForms properties:
https://github.com/dotnet/wpf/blob/709eb9edc5e7ec086a74e136118a39003bc04990/packaging/Microsoft.NET.Sdk.WindowsDesktop/targets/Microsoft.NET.Sdk.WindowsDesktop.props#L74-L81

Since you cannot use the WindowsDesktop SDK for C++, you can instead use the corresponding framework references.

It works for us by ignoring the warning in the compiler settings of the project

The C++ compiler team is aware of spurious warnings when compiling against WPF or windows forms and is looking at fixing it.

cc @wli3 @olgaark @tgani-msft

I think there may be a problem in RAR (or an adjacent target) failing to resolve WindowsBase correctly.

Rather than resolving WindowsBase.dll from Microsoft.WindowsDesktop.App.Ref, the build seems to allow WindowsBase.dll from Microsoft.NetCore.App.Ref to win during reference resolution.

Project: ApngImage.zip
Build logs: msbuild.zip

There is an active discussion about this in the Developer Community at https://developercommunity.visualstudio.com/content/problem/856860/ccli-net-core-unable-to-reference-wpf-classes.html.

In a simple .NET Core project with <FrameworkReferenceInclude="Microsoft.WindowsDesktop.App.WPF" />, we get build orchestration like this:

  • Build

    • CoreBuild

    • GenerateBuildDependencyFile



      • _HandlePackageFileConflicts


      • Encountered conflict between 'Reference:C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\3.1.0\ref\netcoreapp3.1\WindowsBase.dll' and 'Reference:C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.1.0\ref\netcoreapp3.1\WindowsBase.dll'. Choosing 'Reference:C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.1.0\ref\netcoreapp3.1\WindowsBase.dll' because file version '4.800.119.56501' is greater than '4.700.19.56404'.



Simple .NET Core project with FrameworkReference to WPF: ClassLibrary1.zip
Logs showing correct WindowsBase resolution: msbuild-good.zip

In the vcxproj build orchestration, nobody seems to be chaining into _HandlePackageFileConflicts.

/cc @nguerrera, @amit-kabra

@wli3

Maybe a casualty of not supporting PackageReference in C++

I think this can be moved to dotnet/sdk.

Or maybe files as a distinct issue there. I think https://github.com/dotnet/wpf/issues/2117#issuecomment-575424746 is a better description for what needs fixing

From @wli / https://github.com/dotnet/sdk/issues/4204#issuecomment-580387384

status update

Thank you, yes. It is the exactly the missing target. It was unintentionally disabled as part of workaround for C++/CLI not supporting nuget package reference. I am investigating a proper fix

I'll keep this issue open for now, but for those interested in updates, please subscribe to https://github.com/dotnet/sdk/issues/4204 for the latest on this issue.

The fix is checked in expecting VS 16.5. Please reference https://github.com/dotnet/sdk/pull/4245 for workaround

Was this page helpful?
0 / 5 - 0 ratings