How can I reference WindowsBase in .Net5?

5.1k views Asked by At

I am writing a .NET 5 class library that contains the following code:

public T[] ConvertToArray<T>(BitmapFrame frame, int samplesPerPixel) where T:struct
{
    var pixelWidth = frame.PixelWidth;
    var array = new T[pixelWidth  * frame.PixelHeight * samplesPerPixel];
    var stride = pixelWidth * Marshal.SizeOf(typeof(T)) *samplesPerPixel;
    frame.CopyPixels(array,stride,0);//this line prevents the code from compiling
    return array;
}

The line with frame.CopyPixels gives the following compilation error:

CS7069 Reference to type 'Freezable' claims it is defined in 'WindowsBase', but it could not be found. Module 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf385ad364e35' should be referenced

I have been trying to add a reference to WindowsBase.dll from this path: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8\WindowsBase.dll

But I get the following error: The reference is invalid or unsupported

The weird part is that Freezable is listed under .NET 5 in Microsoft's documentation. Maybe the issue is that I am using WindowsBase.dll intended for .NET Framework, but I don't know where the .NET 5 version would be.

Any ideas how I can solve this and get my code to compile?

Thanks!

1

There are 1 answers

1
Nigel On BEST ANSWER

I found a solution, but it makes the class library only compatible with windows applications.

In the project file change this:

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

To this:

  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>True</UseWPF>
  </PropertyGroup>