-1

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!");

Dialog

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");
yuzh
  • 1
  • 2
  • A window class doesn't have `XamlRoot` by default. Are you sure you posted a reproducible code? – Andrew KeepCoding Aug 09 '23 at 14:57
  • Before the function, I defined it. `public XamlRoot XamlRoot { get; private set; }` – yuzh Aug 09 '23 at 15:12
  • What are you setting in the `XamlRoot` property that you implemented on the `MainWindow` class? Does the `MainWindow` has a `Page` as content? – Andrew KeepCoding Aug 09 '23 at 22:37
  • You are saying “Page” in your comments but your code says MainWindow.ShowAsync. Are you actually using a static method in your Window class? Also, about XamlRoot, you need to use the one defined by WinUI, not a property that you define. – sjb-sjb Aug 25 '23 at 09:59

0 Answers0