0

I'm using inside my static setup class the following method in my Winui3 App:

private static async void CheckDocumentsCopyDialog(string targetPath)
    {
        var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForViewIndependentUse();
        ContentDialog checkDocumentsCopy = new ContentDialog()
        {
            Title = Constants.App.Name + " " + resourceLoader.GetString("MsgSetupCheckDocumentHeader"),
            Content = resourceLoader.GetString("MsgSetupCheckDocumentsCopy") + " " + targetPath + ".",
            CloseButtonText = "Ok"
        };
        await checkDocumentsCopy.ShowAsync();
    }

As far as i know it follows Microsofts WinUI3 documentation. But i'm getting:

enter image description here

Maybe anyone knows, what i can do to fix it?

Sascha Manns
  • 2,331
  • 2
  • 14
  • 21
  • Perhaps this can help? https://github.com/microsoft/Xaml-Controls-Gallery/issues/605 – Nick Apr 02 '22 at 08:56

1 Answers1

1

You must set the XamlRoot property of the ContentDialog to a valid XamlRoot.

The easiest way to do this from a static method would be to make the m_window field in the App.xaml.cs file internal and then access the window's XamlRoot using this field, e.g.:

...
checkDocumentsCopy.XamlRoot = (App.Current as App)?.m_window.Content.XamlRoot;
await checkDocumentsCopy.ShowAsync(ContentDialogPlacement.InPlace);

App.xaml.cs:

public partial class App : Application
{
    ...
    protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
    {
        m_window = new BlankWindow2();
        m_window.Activate();
    }

    internal Window m_window;
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Wgere do you get the BlankWindow2 class from? In addition, Visual Studio says, that Window does not have the Property Content for me – TheTanic Nov 29 '22 at 09:35