如何从一个视图中切换视图? [英] How do I switch views from within a view?
问题描述
我在做一个WPF应用程序利用的MVVM光框架。
I'm making a WPF application making use of the MVVM light framework.
我试图做的是在一个视图中登录表单,当用户按下一个按钮,视图中它启动了连接视图模型一个LoginCommand。从那里,我不是要启动它包含应用程序的其余部分,或者简单地切换从同一个窗口视图的新窗口。
What I'm trying to do is have a login form in a view, when the user presses a button within that view it launches a LoginCommand for the attached ViewModel. From there I either want to launch a new window which holds the rest of the application, or simply switch views from the same window.
目前我有它,以便有一个视图叫的MainView有内部势必视图1内容的控制。但是切换到视图2我需要把按钮此有关的MainView,而不是在视图1属于它的地方。
Currently I have it so that there is a view called MainView which has a content control inside bound to View1. However to switch to View2 I need to put the button for this on MainView, and not within View1 where it belongs.
任何意见?
推荐答案
通常我做这一块的两种方式:
Usually I do this one of two ways:
如果登录窗口是一次性启动应用程序之前需要的东西,我就会把它放在应用
对象
If the login window is a one-time thing required before starting the application, I will put it in the OnStartup()
method of the Application
object
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Login
var login = new LoginDialog();
var loginVm = new LoginViewModel();
login.DataContext = loginVm;
login.ShowDialog();
if (!login.DialogResult.GetValueOrDefault())
{
// Error is handled in login class, not here
Environment.Exit(0);
}
// If login is successful, show main application
var app = new ShellView();
var appModel = new ShellViewModel();
app.DataContext = viewModel;
app.Show();
}
另一种方式,我通常做,这是通过 ShellViewModel
或 ApplicationViewModel
它处理我所有的窗口管理。此方法使用的DataTemplates
来定义每个屏幕,并且采用了 ContentControl中
作为当前画面的占位符 ShellView
或 ApplicationView
。
The other way I usually do this is through a ShellViewModel
or ApplicationViewModel
which handles all my window management. This method uses DataTemplates
to define each screen, and uses a ContentControl
as a placeholder for the current screen in the ShellView
or ApplicationView
.
我通常与事件结合本某种一样的系统微软棱镜的 EventAggregator
,所以它可以侦听特定类型的消息,如了OpenWindow
或 CloseWindow
消息。如果你有兴趣,我有一个博客帖子大约的ViewModels 这之间的沟通应该给你一个什么样的事件系统,看起来像一个更好的主意。
I usually combine this with an Event System of some kind, like Microsoft Prism's EventAggregator
, so it can listen for messages of a specific type, such as OpenWindow
or CloseWindow
messages. If you're interested, I have a blog post about Communication between ViewModels that should give you a better idea of what an event system looks like.
例如,我的 ShellViewModel
可以通过显示启动 LoginViewModel
(A 的DataTemplate
是用来告诉WPF绘制 LoginViewModel
LoginView ),它会订阅接收类型的消息 SuccessfulLogin $ C> $ C>。一旦
LoginViewModel
广播 SuccessfulLogin
消息,在 ShellViewModel
将关闭 LoginViewModel
并用 ApplicationViewModel
替换它。你可以看到在我的导航与MVVM这样的一个例子
For example, my ShellViewModel
might start by displaying a LoginViewModel
(a DataTemplate
is used to tell WPF to draw the LoginViewModel
with the LoginView
), and it would subscribe to receive messages of type SuccessfulLogin
. Once the LoginViewModel
broadcasts a SuccessfulLogin
message, the ShellViewModel
will close the LoginViewModel
and replace it with the ApplicationViewModel
. You can see an example of this in my article on Navigation with MVVM
这篇关于如何从一个视图中切换视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!