简体   繁体   English

从 App.xaml.cs WPF 依次启动两个窗口

[英]Start two windows in sequence from App.xaml.cs WPF

I want to let the user login before the MainWindow shows up.我想让用户在MainWindow出现之前登录。

The first window shows up, works fine and after closing all other WindowName.ShowDialog() calls do not work.第一个窗口出现,工作正常,关闭所有其他WindowName.ShowDialog()调用后不起作用。

My code: In app.xaml I removed StartupUri and replaced it with我的代码:在 app.xaml 中,我删除了StartupUri并将其替换为

Startup="Application_Startup"

Which looks like this (App.xaml.cs):看起来像这样(App.xaml.cs):

private void Application_Startup(object sender, StartupEventArgs e)
{
    Window1 window1 = new Window1();
    window1.ShowDialog();

    MainWindow window2 = new MainWindow();
    window2.ShowDialog();
}

I have two completly untouched windows (besides the title) second one looks the same:我有两个完全未受影响的窗口(除了标题)第二个看起来一样:

<Window x:Class="Delete_Me.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Delete_Me"
        mc:Ignorable="d"
        Title="First window" Height="300" Width="300">
    <Grid>

    </Grid>
</Window>

Code behind is also untouched.背后的代码也未受影响。

Question How can I start two windows from App.xaml.cs in sequence?问题如何从 App.xaml.cs 依次启动两个窗口? Or what would be a better way to do this?或者有什么更好的方法来做到这一点?

My approaches I tried to swap out the two windows, which didn't change anything.我的方法是尝试换掉两个窗口,但没有任何改变。

Of course I could start the LoginForm in the contructor / OnLoaded from the MainWindow , but that does not seem to be a beautiful solution to the problem.当然,我可以从MainWindow的构造OnLoaded / OnLoaded启动LoginForm ,但这似乎不是问题的完美解决方案。

I would like to not even start the MainWindow , if the authentication fails.如果身份验证失败,我什至不想启动MainWindow

Any suggestions?有什么建议么? Thank you.谢谢你。

A WPF application quits if no more window is existing.如果不存在更多窗口,WPF 应用程序将退出。 This means your application is stopped after closing the first window before the second window is created.这意味着您的应用程序在创建第二个窗口之前关闭第一个窗口后停止。

To solve your problem you need to create the second window before closing the first one.要解决您的问题,您需要在关闭第一个窗口之前创建第二个窗口。 Just change your code to只需将您的代码更改为

Window1 window1 = new Window1();
MainWindow window2 = new MainWindow();

window1.ShowDialog();
window2.ShowDialog();

to fix it.修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM