WPF在主窗口之前显示对话框 [英] WPF showing dialog before main window

查看:245
本文介绍了WPF在主窗口之前显示对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我尝试的(显然曾经工作过,但不再是):



XAML

 <应用程序... 
Startup =Application_Startup>

应用

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

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

解决方案

>

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

 <应用程序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)
{
//关闭对话框关闭
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

var dialog = new DialogWindow();

if(dialog.ShowDialog()== true)
{
var mainWindow = new MainWindow(dialog.Data);
//重新启用正常关机模式。
Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
Current.MainWindow = mainWindow;
mainWindow.Show();
}
else
{
MessageBox.Show(无法加载数据,错误,MessageBoxButton.OK);
Current.Shutdown(-1);
}
}
}


How one can show dialog window (e.g. login / options etc.) before the main window?

Here is what I tried (it apparently has once worked, but not anymore):

XAML:

<Application ...
    Startup="Application_Startup">

Application:

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

Outcome: myDialogWindow is shown first. When it is closed, the Window1 is shown as expected. But as I close Window1 the application does not close at all.

解决方案

Here's the full solution that worked for me:

In App.xaml, I remove the StartupUri stuff, and add a Startup handler:

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

In App.xaml.cs, I define the handler as follows:

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在主窗口之前显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆