dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.08k stars 1.17k forks source link

BitmapImage issue: Reference to type 'Freezable' claims it is defined in 'WindowsBase' #2341

Closed gdunit closed 4 years ago

gdunit commented 4 years ago

.NET Core Version: 3.1 Windows version: v1903 Does the bug reproduce also in WPF for .NET Framework 4.8?: No Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No

Problem description: When creating a new .net core console app (3.1) and using BitmapImage class BeginInit() or EndInit() methods, compiler errors are generated relating to the type Freezable missing from WindowsBase.dll.

Minimal repro:

weltkante commented 4 years ago

Add references to PresentationCore.dll and WindowsBase.dll (tried framework 4.7.2 and 4.8 versions)

Thats not going to work, you said above you are creating a .NET Core project. There's a reason why VS doesn't offer to add those references, WPF Core/Desktop assemblies are not exchangeable, you need to use the right kind for your framework.

You probably want to add a line <UseWPF>true</UseWPF> in the project files properties section instead of adding references manually. Or create a WPF project in the first place (dotnet new wpf) instead of a console app.

gdunit commented 4 years ago

Thanks, OK so I guessI have misunderstood how this works.

What I am really trying to do is use the BitmapImage classes within System.Windows.Media.Imaging in a .NET core application because they leverage the Windows Imaging Component, which outperforms other imaging libraries in some areas. Previously this was not possible, but my hope was that the WPF port would allow some options.

I tried adding <useWPF>true</useWPF> in the project file, but this does not seem to provide access to the System.Windows.Media namespace; only referencing the assemblies directly (which I now understand is not going to work) resolves it, but with the initial exception evident.

Are there any other possibilities to achieve usage of BitmapImage in .NET Core?

rladuca commented 4 years ago

Add a FrameworkReference.

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

You want this in conjunction with UseWPF as that tells the build to pull in the WPF reference assemblies (amongst other things).

gdunit commented 4 years ago

Awesome! Works like a charm. Thanks both for your help