WPF MVVM - 如何在单击按钮时从 MainWindowViewModel 显示视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5829413/
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
WPF MVVM - How to Show a view from MainWindowViewModel upon Clicking on button
提问by KillerFish
Possible Duplicate:
The best approach to create new window in WPF using MVVM
Hello Friends,
你好朋友,
I have two view MainWindowViewand AddCustomerView. I have menu containing buttons in MainwindowView.xmal.
我有两个视图MainWindowView和AddCustomerView。我在 MainwindowView.xmal 中有包含按钮的菜单。
How could i popup AddCustomerView from MainWindowViewModel by clicking on button.
如何通过单击按钮从 MainWindowViewModel弹出AddCustomerView。
My App.xmal.cs for Startup code is..
我的 App.xmal.cs 启动代码是..
base.OnStartup(e);
MainWindow window = new MainWindow();
var viewModel = new MainWindowViewModel();
window.DataContext = viewModel;
window.Show();
What is the code for showing AddCustomerView in buttonexecute code.
在按钮执行代码中显示 AddCustomerView 的代码是什么。
public void AddNewCustomerWindowExecute() //This is button handler
{
// How to show AddCustomerView from MainWindowViewModel
}
回答by mak
Handle it in the view
在视图中处理
Probably the most simple approach.
可能是最简单的方法。
private void AddCustomerView_Click(object sender, RoutedEventArgs e)
{
AddCustomerView view = new AddCustomerView(data);
view.Show();
}
ViewModel exposes an event
ViewModel 暴露一个事件
This has one drawback: it requires lots of manual coding.
这有一个缺点:它需要大量的手动编码。
public class MainWindowViewModel
{
public event EventHandler AddCustomerViewShowed;
public void AddNewCustomerWindowExecute()
{
if (AddCustomerViewShowed != null)
AddCustomerViewShowed(this, EventArgs.Empty);
}
}
Handle it in the view
在视图中处理
var viewModel = new MainWindowViewModel();
viewModel.AddCustomerViewShowed += (s, e) => new AddCustomerView(data).Show();
Controller that handles all your views
处理所有视图的控制器
public class Controller : IController
{
public void AddCustomer()
{
AddCustomerView view = new AddCustomerView(data);
view.Show();
}
}
public class MainWindowViewModel
{
IController controler;
public MainWindowViewModel(IController controller)
{
this.controller = controller;
}
public void AddNewCustomerWindowExecute()
{
controller.AddCustomer();
}
}
Mediator pattern
中介模式
Some MVVM frameworks (e.g. MVVM Light) use this pattern.
一些 MVVM 框架(例如MVVM Light)使用这种模式。
public class App // or in the view or somewhere else
{
public void RegisterMessenger()
{
Messenger.Default.Register<AddCustomerMessage>(this, ProcessAddCustomerMessage);
}
private void ProcessAddCustomerMessage(AddCustomerMessage message)
{
AddCustomerView view = new AddCustomerView(data);
view.Show();
}
}
public class MainWindowViewModel
{
public void AddNewCustomerWindowExecute()
{
Messenger.Default.Send(new AddCustomerMessage(...));
}
}
回答by silverfighter
Check out this "deep dive MVVM video". Laurent Bugnion shows the Concept of the IDialogService and explains the concepts very well... plus the source code should also be available... The concepts apply also to wpf
查看此“深入了解 MVVM 视频”。Laurent Bugnion 展示了 IDialogService 的概念并很好地解释了这些概念......加上源代码也应该可用......这些概念也适用于 wpf
http://channel9.msdn.com/Events/MIX/MIX11/OPN03
http://channel9.msdn.com/Events/MIX/MIX11/OPN03
HTH
HTH