Search code examples
c#winui-3

telling apart Rect class in different assemblies of c# eco system


I am working on a WinUI3 project, in the dependencies my frameworks are stated as Microsoft.NETCore.App and Microsoft.Windows.SDK.Net.Ref. I need a class to represent a rectangular geometry. I have found an already defined class: https://learn.microsoft.com/en-us/dotnet/api/system.windows.rect?view=windowsdesktop-9.0

It seems this class is within WindowsBase assembly that I have within my project under `Microsoft.NETCore.App framework, but I don't know how to reference it https://referencesource.microsoft.com/#WindowsBase,namespaces

Online it is listed under Windows Desktop 9 (which means Windows Presentation Foundation?), anyway I cannot reference this class from within my project.

Can however reference Windows.Foundation.Rect https://learn.microsoft.com/en-us/uwp/api/windows.foundation.rect?view=winrt-26100 but it misses much of the functionality I need such as IntersectsWith(Rect), Offset, etc...

Also can reference System.Drawing.Rectangle https://learn.microsoft.com/en-us/dotnet/api/system.drawing.rectangle?view=net-8.0 which is listed under .NET but in the description says is designed for Windows Forms which I heard is to be discontinued

So first, I don't understand why I can access one but not the other, also what should I do? To be honest I spent so much time trying to make sense of it all that perhaps it would be better to implement everything with my own code.


Solution

  • as Simon Mourier Commented the solution was to add <UseWPF>true</UseWPF> to .csproj project file

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
        <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
        <UseWPF>true</UseWPF>
    

    However it breaks the project as WinUI auto generated code didn't work propertly.

    Was related to How can I reference WindowsBase in .Net5?