3

我正在尝试在我的 UWP (WinUI3) 应用程序中创建登录系统,当我尝试启动登录内容对话框时,它崩溃并抛出此错误:

System.ArgumentException:“值不在预期范围内。”

await messageDialog.ShowAsync();

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

内容对话框 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>

代码隐藏:

public LoginDialog()
{
    this.InitializeComponent();
}
4

1 回答 1

7

等到窗口的内容加载完毕,然后XamlRoot设置ContentDialog.

这是您在启动时显示对话框的方式:

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;
于 2021-06-21T14:37:38.213 回答