如何打开WPF窗口,关闭它然后打开另一个窗口而不关闭应用程序而不使用框架导航? [英] How does one open a WPF window, close it and then open another window without the application closing and not using frame navigation?

查看:294
本文介绍了如何打开WPF窗口,关闭它然后打开另一个窗口而不关闭应用程序而不使用框架导航?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写的应用程序在启动时有一个配置步骤,我在初始化Autofac DI之前收集所需的配置和用户输入。然后我通过调用标准 Window.Close()调用来关闭所述窗口。



代码似乎执行正确,但下一个窗口不想显示。似乎 Window.Close()会导致一个事件在调用之后覆盖任何内容。



这里有一些代码可以解释我正在做的事情:



The application I am writing has a configuration step in its startup where I collect required configs and user inputs before I init my Autofac DI. Then I close said window by invoking the standard Window.Close() call.

The code seems to execute correctly but the next window does not want to show. It seems like the Window.Close() results in an event which overrides anything after it has been called.

Here is some more code to shed some light on what I am doing:

var connectionview = new ConnectionView(connectionViewModel);
var result = connectionview.ShowDialog();
if (result.messageResult == MessageDialogResult.No)
               return;

var bootstrapper = new Bootstrapper(tempConnectionContext);
Container = bootstrapper.Bootstrap();
_mainViewModel = Container.Resolve<MainWindowViewModel>();
MainWindow = new MainWindow(_mainViewModel);
MainWindow.Show();





如果有人能指出我的方向也会有所帮助。任何帮助都会很精彩。

谢谢



我的尝试:



我不认为我在正确的位置搜索。



If anyone could point me into a direction too it would help. Any help would be wonderful.
Thanks

What I have tried:

I don't think I am searching in the right places.

推荐答案

对于MVVM,您需要使用代理服务,因为虚拟机不应该访问任何UI,包括Windows。我在类似的问题中回答了这个问题。



请在那里查看我的答案,而不是在此重新发布:如何在C#WPF中动态更改列表框项目大小? [ ^ ]



更新:



这篇文章对你有很大的帮助:异步编程 - 从头开始​​的异步 [ ^ ]
For MVVM, you need to use a proxy service as VMs are not supposed to access any UI, including windows. I have answer this in a similar question.

Please check out my answer there rather than me reposting it here: How to change listbox item size dynamically in C# WPF?[^]

UPDATE:

This article will be of great help to you: Asynchronous Programming - Async from the Start[^]


在Windows窗体和WPF中,都有一个windows的层次结构。您的启动窗口是层次结构树的顶部,显示的任何其他窗口都是子窗口,这些子窗口可以显示自己的子窗口。



一旦关闭树的顶部窗口,您的应用程序与它一起死亡。所以,解决方案相当简单。您将错误的窗口设置为启动窗口。启动窗口应始终是应用程序的主要形式,而不是登录表单,而不是设置对话框,...
In both Windows Forms and WPF, there is a hierarchy to windows. Your startup window is the top of the hierarchy tree and any other windows that show up are children, with those children able to show their own child windows.

Once you close the top window of the tree, your application dies with it. So, the solution is rather simple. You set the wrong window as your startup window. The startup windows should ALWAYS be the main form of your application, not a login form, not a settings dialog, ...


我认为您的应用程序在关闭主窗口后关闭。所以要么不要让第一个窗口成为MainWindow(只是一个Dialog,就像一个splashscreen),要么为你的应用程序使用ShutdownMode =OnExplicitShutdown

I think your application is closed after the Close of the mainwindow. So either don't make the first window the MainWindow (just a Dialog, like a splashscreen) or use ShutdownMode="OnExplicitShutdown"
for you application


这篇关于如何打开WPF窗口,关闭它然后打开另一个窗口而不关闭应用程序而不使用框架导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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