我正在WinUI3 UWP中为Windows11开发一个新的应用程序,我希望显示一个对话框来提供一个安全的操作,如下面的Microsoft: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上的“in”按钮显示该对话框时,当用户单击"ok“返回到MainPage.xaml时,但当我运行程序时,请给我以下错误:”必须将XamlRoot显式设置为无父级的弹出窗口“
我怎么解决这个问题?
谢谢!
发布于 2022-01-10 02:30:34
似乎您正在开发一个WinUI3应用程序。正如@Raymond所提到的,您必须将ContentDialog.添加XamlRoot property
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();
}https://stackoverflow.com/questions/70642963
复制相似问题