2

I want to create a window with acrylic style. I had read the code on WinUI3 Gallery and tried to use AddSystemBackdropTarget() to achieve that.

protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    // m_window = WindowHelper.CreateWindow();
    m_window = new Window();
    Frame rootFrame = GetRootFrame(args);
    // if (m_window.DispatcherQueue != null) // ture
    DesktopAcrylicController acrylicController = new();
    acrylicController.AddSystemBackdropTarget(m_window.As<ICompositionSupportsSystemBackdrop>());
    m_window.Activate();
}

PS: I'm using the template of WinUI3 which was created by Visual Studio.

The exception was:
exception info

About my machine:
OS: Window 11, version 22H2
Windows SDK: Microsoft.WindowsAppRuntime.1.2

I have noticed that the AddSystemBackdropTarget() method must be called on a thread with a DispatcherQueue, so I checked the property to ensure it's not null.
And I also tried TryEnqueue() method to add the AddSystemBackdropTarget() task to the DispatcherQueue. But none of them could solve my problem.

Howie
  • 31
  • 3
  • 1
    You need a `Windows.System.DispatcherQueue` (specific to Windows) on the thread which is not the same as a `Microsoft.UI.Dispatching.DispatcherQueue` (WinUI3) before you call `AddSystemBackdropTarget`. Check out the offical sample here to create one https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/system-backdrop-controller#example-use-mica-in-a-windows-appsdkwinui-3-app – Simon Mourier Nov 27 '22 at 17:29
  • Thanks, the problem was successfully solved! But could you please explain the difference between Windows.System.DispatcherQueue and Microsoft.UI.Dispatching.DispatcherQueue. I'm still wondering why it worked? – Howie Nov 28 '22 at 05:35
  • 1
    WinUI3 has a certain level of independence from Windows. It mainly uses it's own dispatcher queue and has its own composition engine (Visual Layer) for most work in the Microsoft.* namespaces. But Acrylic & Mica are 100% specific to Windows as they use WinRT's Visual Layer (same as UPW one) in the Windows.* namespaces https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/composition – Simon Mourier Nov 28 '22 at 07:49

1 Answers1

1

Thanks again to @Simon Mourier for his answer. The problem has been solved. To solve this problem, you need to use CreateDispatcherQueueController() method which belongs to CoreMessaging.dll. The exception happened because I confused WinUI3 and WinRT visual layer. For more information, please check out the comments above.

EDITing in those comments:

You need a Windows.System.DispatcherQueue (specific to Windows) on the thread which is not the same as a Microsoft.UI.Dispatching.DispatcherQueue (WinUI3) before you call AddSystemBackdropTarget. Check out the official sample here to create one.

WinUI3 has a certain level of independence from Windows. It mainly uses its own dispatcher queue and has its own composition engine (Visual Layer) for most work in the Microsoft.* namespaces. But Acrylic & Mica are 100% specific to Windows as they use WinRT's Visual Layer (same as UWP one) in the Windows.* namespaces https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/composition

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
Howie
  • 31
  • 3