ContentDialog is raising error when we trying to show

129 Views Asked by At

It always shows " This element is already associated with a XamlRoot, it cannot be associated with a different one until it is removed from the previous XamlRoot "

Also its raising error when we set XamlRoot = this.Content.XamlRoot;

Can anybody explain why this behaviour is happening and how could we resolve that ?

1

There are 1 best solutions below

6
SE User On

I would recommend trying this code and then modifying it to your needs:

private async void ShowDialogButton_Click(object sender, RoutedEventArgs e)
{
    ContentDialog dialog = new ContentDialog
    {
        Title = "Hello!",
        Content = "This is a simple ContentDialog.",
        PrimaryButtonText = "OK"
    };

    dialog.PrimaryButtonClick += delegate
    {
        // Do something when the user clicks the primary (OK) button.
    };

    dialog.XamlRoot = this.Content.XamlRoot;  // Set XamlRoot of the dialog.
    await dialog.ShowAsync(); // Show the dialog.
}

But to really answer your question, you'd need to provide your code and more details in your question.