WPF显示主窗口之前的对话框

如何在主窗口之前显示对话框窗口(例如登录/选项等)?

这里是我试过(它显然是has once worked,但不再):

XAML:

<Application ...
    Startup="Application_Startup">

应用:

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        Window1 myMainWindow = new Window1();
        DialogWindow myDialogWindow = new DialogWindow();
        myDialogWindow.ShowDialog();
    }
}

结果:myDialogWindow首先显示。当它关闭时,Window1显示为预期。但是当我关闭Window1应用程序不会关闭。

最佳答案
这里是我的完整解决方案:

在App.xaml中,我删除了StartupUri的东西,并添加了一个启动处理程序:

<Application x:Class="MyNamespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="ApplicationStart">
</Application>

在App.xaml.cs中,我定义了如下处理程序:

public partial class App
{
    private void ApplicationStart(object sender, StartupEventArgs e)
    {
        //Disable shutdown when the dialog closes
        Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

        var dialog = new DialogWindow();

        if (dialog.ShowDialog() == true)
        {
            var mainWindow = new MainWindow(dialog.Data);
            //Re-enable normal shutdown mode.
            Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
            Current.MainWindow = mainWindow;
            mainWindow.Show();
        }
        else
        {
            MessageBox.Show("Unable to load data.", "Error", MessageBoxButton.OK);
            Current.Shutdown(-1);
        }
    }
}

转载注明原文:WPF显示主窗口之前的对话框 - 代码日志