System.ArgumentException: 'Value does not fall within the expected range.' on Content Dialog
Asked Answered
D

1

6

I'm triyng to make a login system in my UWP (WinUI3) app and when I try to lauch de Login Content Dialog it crashes throwing this error:

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

on await messageDialog.ShowAsync();

Code on app.xaml.cs:

protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{

    m_window = new MainWindow();
    m_window.Activate();

    Login();

}

protected async void Login()
{
    var messageDialog = new ContentDialogs.LoginDialog();
    await messageDialog.ShowAsync();

    if (App.Aplicacao.Utilizador == null)
    {
        m_window.Close();
        return;
    }
}

Content Dialog XAML:

<ContentDialog
    x:Class="xizSoft.ContentDialogs.LoginDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:xizSoft.ContentDialogs"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource SystemControlAcrylicWindowBrush}">

    <Grid>
    </Grid>
</ContentDialog>

Code-behind:

public LoginDialog()
{
    this.InitializeComponent();
}
Delossantos answered 17/6, 2021 at 10:58 Comment(1)
I tested it in a normal UWP app. It works correctly. Then I use the default ContentDialog to test in a WinUI3 UWP app. It still works. And the issue only happens to custom ContentDialog and it should be related to WinUI3. You could ask this in WinUI issues in Github. Then WinUI team is actively answering WinUI questions there.Caylor
L
12

Wait until the contents of the window has been loaded and set the XamlRoot property of the ContentDialog.

This is how you would display the dialog on startup:

protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
    m_window = new MainWindow();
    m_window.Activate();

    if (m_window.Content is FrameworkElement fe)
        fe.Loaded += (ss, ee) => Login();
}

protected void Login()
{
    var messageDialog = new ContentDialogs.LoginDialog();
    messageDialog.XamlRoot = m_window.Content.XamlRoot;
    await messageDialog.ShowAsync();

    if (App.Aplicacao.Utilizador == null)
    {
        m_window.Close();
        return;
    }
}

private Window m_window;
Lulalulea answered 21/6, 2021 at 14:37 Comment(2)
This remains an open issue. For more, see: github.com/microsoft/microsoft-ui-xaml/issues/2504Cuspidor
Setting the XamlRoot property fixed it for me, using WinUI 3.Herbivorous

© 2022 - 2024 — McMap. All rights reserved.