Grabacr07 / VirtualDesktop

C# wrapper for the Virtual Desktop API on Windows 11.
MIT License
631 stars 135 forks source link

WinUI 3 and .NET 5 #64

Closed Slion closed 2 years ago

Slion commented 2 years ago

Is there a version for WinUI?

Grabacr07 commented 2 years ago

Please set your project's target framework to net6.0-windows10.0.19041.0, it should work with WinUI 3.

If you need .NET 5 version, set the VirtualDesktop target to net5.0-windows10.0.19041.0 and add <LangVersion>10.0</LangVersion> to make it work.

Slion commented 2 years ago

Using the main NuGet package? … Switched to .NET 6 as you suggested and it compiles using the main NuGet package. However VirtualDesktop.CurrentChanged is not fired. Running on Windows 10. VirtualDesktop.PinWindow throws the following exception:

System.Runtime.InteropServices.COMException
  HResult=0x8002802B
  Message=Element not found. (0x8002802B (TYPE_E_ELEMENTNOTFOUND))
  Source=VirtualDesktop
  StackTrace:
   at WindowsDesktop.Interop.ComWrapperBase`1.InvokeMethod[T](Object[] parameters, String methodName)
   at WindowsDesktop.Interop.Build10240.ApplicationViewCollection.GetViewForHwnd(IntPtr hWnd)
   at WindowsDesktop.Interop.Build10240.VirtualDesktopProvider10240.<>c__DisplayClass15_0.<InitializeCore>b__1(IntPtr x)
   at WindowsDesktop.Interop.Build10240.VirtualDesktopPinnedApps.ArgsWithApplicationView(IntPtr hWnd)
   at WindowsDesktop.Interop.Build10240.VirtualDesktopPinnedApps.IsViewPinned(IntPtr hWnd)
   at WindowsDesktop.VirtualDesktop.PinWindow(IntPtr hWnd)

Looks like the interfaces are not loaded properly?

Grabacr07 commented 2 years ago

How to get the HWND (window handle; IntPtr) in WinUI 3?

at WindowsDesktop.Interop.Build10240.ApplicationViewCollection.GetViewForHwnd(IntPtr hWnd) Message=Element not found. (0x8002802B (TYPE_E_ELEMENTNOTFOUND))

This means that the specified window cannot be found.

Grabacr07 commented 2 years ago

If VirtualDesktop.CurrentChanged is not fired, try accessing other static members. I think it will not work just subscribe to the event, since the internal initialization is triggered by the static method/property access.

e.g.

var current = VirtualDesktop.Current;
Slion commented 2 years ago

How to get the HWND (window handle; IntPtr) in WinUI 3?

WinRT.Interop.WindowNative.GetWindowHandle(this) I know my HWND is valid I'm using it to do a bunch of other stuff and it works.

Slion commented 2 years ago

If VirtualDesktop.CurrentChanged is not fired, try accessing other static members. I think it will not work just subscribe to the event, since the internal initialization is triggered by the static method/property access.

e.g.

var current = VirtualDesktop.Current;

That did the trick, thanks.

Slion commented 2 years ago

This means that the specified window cannot be found.

I could get it working by delaying the PinWindow call until the first window Activated event is fired. If I do it from the window constructor it won't work though the HWND is already valid it's probably not registered yet as desktop window somehow.

Thanks for the amazing support @Grabacr07 ❤🎉 Looks like I'm all set! Great library! I'll be testing it on Windows 11 in the coming days.