Sascha Manns
Sascha Manns

Reputation: 2510

System.ArgumentException: 'Value does not fall within the expected range.' on static class

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?

Upvotes: 0

Views: 1651

Answers (1)

mm8
mm8

Reputation: 169340

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;
}

Upvotes: 1

Related Questions