1

I am making a WinUI 3 app in C++. I tried setting the backdrop of the window to acrylic but it does not work. It crashes with the exception "hresult_error" when calling AddSystemBackdropTarget. I could not find out why this is.

Code:

void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
    {
        auto m_configurationSource = Microsoft::UI::Composition::SystemBackdrops::SystemBackdropConfiguration();
        auto m_Target = this->try_as<Microsoft::UI::Composition::ICompositionSupportsSystemBackdrop>();
        auto desktopAcrylicController = Microsoft::UI::Composition::SystemBackdrops::DesktopAcrylicController();

        if (m_Target == nullptr) {
            MessageBox(NULL, L":(", L":(", MB_OK);
        }

        desktopAcrylicController.AddSystemBackdropTarget(m_Target); // This is where the error is
        desktopAcrylicController.SetSystemBackdropConfiguration(m_configurationSource);
    }
Davide_24
  • 67
  • 7

1 Answers1

0

When you debug, you'll see the error is "0x80070005 : 'Must have a Windows.System.DispatcherQueue on the current thread.'".

This is the same issue as here How to set the acrylic style to window?

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 UWP one) in the Windows.* namespaces.

So, you must create a whole shebang to make it work, the official sample is described here Example: Use Mica in a Windows AppSDK/WinUI 3 app (don't forget to choose C++/WinRT in the upper right corner to get the code for C++ not for C#)

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298