gpt4 book ai didi

uwp - WinUI 3 UWP - 对话框

转载 作者:行者123 更新时间:2023-12-05 02:35:02 25 4
gpt4 key购买 nike

我正在 WinUI3 UWP 中为 Windows 11 开发一个新应用程序,我想显示一个对话框以提供安全操作,如 Microsoft Docs 的这个示例:https://learn.microsoft.com/en-us/windows/apps/design/controls/dialogs-and-flyouts/dialogs

private async void DisplayNoWifiDialog()
{
ContentDialog noWifiDialog = new ContentDialog
{
Title = "No wifi connection",
Content = "Check your connection and try again.",
CloseButtonText = "Ok"
};

ContentDialogResult result = await noWifiDialog.ShowAsync();
}

当用户单击我的 MainPage.xaml 上的按钮时显示该对话框,当用户单击“确定”时返回到 MainPage.xaml,但是当我运行我的程序时给我这个错误:“XamlRoot 必须显式设置为无父级弹出”

我该如何解决?

谢谢!

最佳答案

您似乎正在开发 WinUI3 应用程序。正如@Raymond Chen 提到的,您必须将 XamlRoot 属性 添加到 ContentDialog

Xaml:

  <StackPanel x:Name="MyPanel" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>

代码隐藏:

 private async void myButton_Click(object sender, RoutedEventArgs e)
{
ContentDialog noWifiDialog = new ContentDialog
{
Title = "No wifi connection",
Content = "Check your connection and try again.",
CloseButtonText = "Ok"
};
//set the XamlRoot property
noWifiDialog.XamlRoot = MyPanel.XamlRoot;

ContentDialogResult result = await noWifiDialog.ShowAsync();
}

关于uwp - WinUI 3 UWP - 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70642963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com