I have a UWP app that has multiple AppWindows. I want to display a common contentdialog that is defined in XAML on the MainPage on this AppWindow. I tried setting the XamlRoot but get this error: System.Exception: 'Catastrophic failure Cannot change XamlRoot when it is already set'.

Here is my XAML Code:

<ContentDialog x:Name="DetailsDialog" x:FieldModifier="public" Title="Details" CloseButtonText="Close" Margin="10,50,-10,-50" Visibility="Collapsed">
        <ScrollViewer x:Name="DetailsScrollViewer" x:FieldModifier="Public">
            <StackPanel x:Name="DetailsPanel" x:FieldModifier="public">
                <Frame x:Name="DetailsContentFrame" x:FieldModifier="public" Padding="0,0,0,0" IsTabStop="True">
                </Frame>
            </StackPanel>
        </ScrollViewer>
    </ContentDialog>

This is the AppWindow's Code Behind to show the ContentDialog on the AppWindow:

            PrintableArea.Children.Remove(ProcedureNotesPanel);
        mainPage.DetailsPanel.Children.Add(ProcedureNotesPanel);
        ProcedureNotesPanel.Visibility = Visibility.Visible;
        mainPage.DetailsDialog.Title = title;
        mainPage.DetailsDialog.Visibility = Visibility.Visible;
        mainPage.DetailsDialog.XamlRoot = null;
        mainPage.DetailsDialog.XamlRoot = ((FrameworkElement)sender).XamlRoot;
        await mainPage.DetailsDialog.ShowAsync();
        ProcedureNotesPanel.Visibility = Visibility.Collapsed;
        mainPage.DetailsPanel.Children.Remove(ProcedureNotesPanel);
        PrintableArea.Children.Add(ProcedureNotesPanel);

What am I doing wrong? I tried setting the XamlRoot to null first but even that throws the same exception.

Thanks.

1

There are 1 best solutions below

0
On

After some more research and trial and error, I was able to achieve the intended outcome by creating a new ContentDialog instead of using an existing one on MainPage.

public async Task<ContentDialogResult> ShowDialog(FrameworkElement ControlToShow, FrameworkElement target, string Title, string CloseButtonText = "Close", ContentDialogPlacement placement = ContentDialogPlacement.Popup)
    {
        try
        {
            var dialog = new ContentDialog()
            {
                Title = Title,
                CloseButtonText = CloseButtonText,
            };
            var panel = new StackPanel() { Name = "CustomContentDialogPanel" };
            var parent = ((Grid)ControlToShow.Parent);

            parent.Children.Remove(ControlToShow);
            panel.Children.Add(ControlToShow);
            ControlToShow.Visibility = Visibility.Visible;
            dialog.Content = panel;
            dialog.XamlRoot = target.XamlRoot;
            var result = await dialog.ShowAsync(placement);
            panel.Children.Remove(ControlToShow);
            parent.Children.Add(ControlToShow);
            ControlToShow.Visibility = Visibility.Collapsed;

            return result;
        }
        catch (Exception ex)
        {
            return ContentDialogResult.None;
        }
    }