WPF 在主窗口前显示对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1539958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:04:32  来源:igfitidea点击:

WPF showing dialog before main window

wpfdialog

提问by Ciantic

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:

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.

结果:首先显示 myDialogWindow。当它关闭时,Window1 会按预期显示。但是当我关闭 Window1 时,应用程序根本没有关闭。

回答by Gleno

Here's the full solution that worked for me:

这是对我有用的完整解决方案:

In App.xaml, I remove the StartupUristuff, and add a Startuphandler:

App.xaml 中,我删除了StartupUri这些东西,并添加了一个Startup处理程序:

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

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

回答by Ciantic

Okay apologizes, here is the solution:

好的道歉,这是解决方案:

My original question worked almost, only one thing to add, remove the StartupUri from the Application XAML and after that add the Show to main window.

我原来的问题几乎有效,只需要添加一件事,从应用程序 XAML 中删除 StartupUri,然后将 Show 添加到主窗口。

That is:

那是:

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

Above, StartupUri removed.

上面,StartupUri 删除了.

Add myMainWindow.Show()too:

添加myMainWindow.Show()太:

public partial class App : Application
{

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        Window1 myMainWindow = new Window1();
        DialogWindow myDialogWindow = new DialogWindow();
        myDialogWindow.ShowDialog();
        myMainWindow.Show();
    }

}

回答by James Durda

WPF sets App.Current.MainWindowto the first window opened. If you have control over the secondary window constructor, just set App.Current.MainWindow = Nullthere. Once your main window is constructed, it will be assigned to the App.Current.MainWindowproperty as expected without any intervention.

WPF 设置App.Current.MainWindow为打开的第一个窗口。如果您可以控制辅助窗口构造函数,只需在App.Current.MainWindow = Null那里设置。主窗口构建完成后,它将App.Current.MainWindow按预期分配给属性,无需任何干预。

public partial class TraceWindow : Window
{
    public TraceWindow()
    {
        InitializeComponent();
        if (App.Current.MainWindow == this)
        {
            App.Current.MainWindow = null;
        }
    }
}

If you don't have access, you can still set MainWindowwithin the main window's constructor.

如果您无权访问,您仍然可以MainWindow在主窗口的构造函数中进行设置。

回答by Patrick

If you put Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;into the constructor of the dialog, and add

如果你放入Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;对话框的构造函数中,并添加

protected override void OnClosed(EventArgs e) {
    base.OnClosed(e);
    Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
}

into the dialog class, you don't need to worry about making any changes to the default behaviour of the application. This works great if you want to just snap a login screen into an already-existing app without tweaking the startup procedures.

进入对话框类,您无需担心对应用程序的默认行为进行任何更改。如果您只想在不调整启动程序的情况下将登录屏幕捕捉到已经存在的应用程序中,这非常有用。

回答by Steven Robbins

So you want to show one window, then another, but close down the app when that window is closed? You may need to set the ShutdownModeto OnMainWindowClose and set the MainWindowto Window1, along the lines ok:

因此,您想先显示一个窗口,然后再显示另一个窗口,但在该窗口关闭时关闭应用程序?您可能需要将ShutdownMode设置为 OnMainWindowClose 并将MainWindow设置为 Window1,沿着 ok 行:

Window1 myMainWindow = new Window1();
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
Application.Current.MainWindow = myMainWindow;
DialogWindow myDialogWindow = new DialogWindow();
myDialogWindow.ShowDialog();

回答by Muad'Dib

here, do it like this. this will actaully change your main window and will work properly w/o having to change settings of your application object.

在这里,这样做。这将实际更改您的主窗口,并且无需更改应用程序对象的设置即可正常工作。

make sure to remove the event handler for application startup and to set your StartupUri in your app.xaml file.

确保删除应用程序启动的事件处理程序并在 app.xaml 文件中设置您的 StartupUri。

public partial class App : Application
{
   bool init = false;
   protected override void OnActivated(EventArgs e)
   {
      base.OnActivated(e);
      if (!init)
      {
         this.MainWindow.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
         init = true;
      }
   }

   void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
   {
      Window toClose = this.MainWindow;
      this.MainWindow = new Window2();
      this.MainWindow.Show();
   }
}