英文:
App crashes when setting Acrylic background | WinUI 3
问题
我在用C++制作WinUI 3应用程序。我尝试将窗口的背景设置为亚克力,但没有成功。调用AddSystemBackdropTarget时,它崩溃并显示“hresult_error”异常。我找不到原因。
代码:
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); // 这里是错误发生的地方
desktopAcrylicController.SetSystemBackdropConfiguration(m_configurationSource);
}
英文:
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);
}
答案1
得分: 0
调试时,你会看到错误是“0x80070005:'必须在当前线程上具有 Windows.System.DispatcherQueue。'”。
这与此处的问题相同 https://stackoverflow.com/questions/74591734/how-to-set-the-acrylic-style-to-window
WinUI3 与 Windows 有一定的独立性。它主要使用自己的调度程序队列,并在 Microsoft.*
命名空间中使用自己的组合引擎(“Visual Layer”)执行大部分工作。但是,Acrylic 和 Mica 完全依赖于 Windows,因为它们使用 WinRT 的 Visual Layer(与 UWP 相同)在 Windows.*
命名空间中。
因此,你必须创建一个完整的设置才能使其正常工作,官方示例在这里描述了这个过程 示例:在 Windows AppSDK/WinUI 3 应用中使用 Mica(不要忘记在右上角选择 C++/WinRT 以获取 C++ 代码,而不是 C# 代码)。
英文:
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 https://stackoverflow.com/questions/74591734/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#)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论