admin管理员组

文章数量:1435859

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: .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 ,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 .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 .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.

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/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/#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/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/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.

Share Improve this question edited Nov 15, 2024 at 19:23 Eliy Arlev asked Nov 15, 2024 at 19:09 Eliy ArlevEliy Arlev 5782 gold badges4 silver badges16 bronze badges 7
  • WinUI3, Winforms and WPF are 3 "competiting" UI framework, if you will. In general if you work with WinUI3 you don't reference assemblies from the others (PS: none of these 3 is discontinued, Winforms sticks to "old" HWND+GDI windows, WPF is XAML + DirectX9, WinUI3 is the most modern and supports other non-.NET languages like C++ but still lacks some features). Anyway the easiest way to reference System.Windows.Rect or System.Drawing.Rectangle is to create a library, reference WPF or Winforms from there and then reference this newer library from your WinUI3 project. – Simon Mourier Commented Nov 15, 2024 at 19:51
  • See this for more background on WinUI3 thomasclaudiushuber/2021/02/05/… – Simon Mourier Commented Nov 15, 2024 at 19:53
  • As I said, Windows.Foundation.Rect and System.Drawing.Rectangle, are accessible. I really want is to reference the Rect in WindowsBase and I feel I am really close. I would assume that this assembly System.Windows namespace is overshadowed by the same namespace from other assemblies within the framework. But, I can't figure out how can i access it. Thanks for the link – Eliy Arlev Commented Nov 15, 2024 at 20:02
  • In "WinUI", one uses the Microsoft.UI.Xmal.Shapes.Rectangle for "drawing"; and can use the Windows.Foundation.Rect for "geometry". The method in question is "Intersect"; not "IntersectsWith". A further clue is that WinUI uses the "Foundation.Point" versus the "Drawing.Point". I use the FrameworkElement (Canvas left, top; Width and Height) to create the corresponding Rect when I need it. (Add it as an extension method, for example). – Gerry Schmitz Commented Nov 15, 2024 at 20:45
  • 1 Re-read what I said. Add a class library project with <TargetFramework>net8.0-windows</TargetFramework> for example and <UseWPF>true<UseWPF> in it, reference that project from your WinUI3 project and you will be able to use System.Windows.Rect from WindowsBase. I never mentioned Windows.Foundation.Rect. – Simon Mourier Commented Nov 16, 2024 at 5:17
 |  Show 2 more comments

1 Answer 1

Reset to default 0

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?

本文标签: winui 3telling apart Rect class in different assemblies of c eco systemStack Overflow