In the page that defines the function, i use the code below:
public XamlRoot XamlRoot { get; private set; }
public async void ShowDialog(string content)
{
ContentDialog dialog = new()
{
XamlRoot = this.XamlRoot,
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
Title = "提示",
PrimaryButtonText = "确定",
CloseButtonText = "取消",
DefaultButton = ContentDialogButton.Primary,
Content = new ContentDialogContent(content)
};
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
dialog.XamlRoot = this.Content.XamlRoot;
_ = await dialog.ShowAsync();
}
and this is the way to use it in its page(it works well, the dialog shows):
ShowDialog("Congratulations!");
In the page that i want to call the function, i use the code below(when a button is clicked, which doesn't work...):
private void UnlockNowButton_Click(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = new MainWindow();
mainWindow.ShowDialog("Dialog content");
}
However, when i run the program, there're no errors in the error list.
When i tried to use it, the dialog didn't show up.
I tried to looking for problems in the page that calls the function, but failed.
The code looks really well...
MainWindow mainWindow = new MainWindow();
mainWindow.ShowDialog("Dialog content");