[英]Navigate through UserControl with MVVM LIGHT (WPF)
First of all I apologize for my poor english which is not my first language. 首先,我为我的英语不佳而道歉,这不是我的母语。
I'm new in MVVM so my question is probably a very newbie one ;) 我是MVVM的新手,所以我的问题可能是一个非常新手;)
I'm encountering some issue with switching View in a C# Application using WPF and MVVM LIGHT. 我在使用WPF和MVVM LIGHT在C#应用程序中切换视图时遇到一些问题。 I've read a lot of articles but i still can't figured out how to do it in a clean way.
我读了很多文章,但我仍然想不出一个干净的方法。
So here is my question: What is the best way to achieve the navigation between UserControl contained in a MainWindows, assuming that: 所以这是我的问题:假设在MainWindows中实现UserControl之间导航的最佳方法是:
Since nobody answers to my question, this is what I finally did. 由于没有人回答我的问题,所以我终于做到了。 It might not be the best way but at least it works well.
这可能不是最好的方法,但至少效果很好。 I hope it'll helps some newbies like me who are struggling learning this pattern:
我希望它能帮助像我这样努力学习这种模式的一些新手:
Put a CurrentViewModel Object in the MainViewModel: 将CurrentViewModel对象放入MainViewModel中:
public class MainViewModel : ViewModelBase,IMainViewModel
{
/* Other piece of code */
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
_currentViewModel = value;
RaisePropertyChanged(() => CurrentViewModel);
}
}
}
Obviously bind this to the Mainview (Just the relevant code): 显然将其绑定到Mainview(只需相关代码):
<UserControl Content="{Binding Path=CurrentViewModel}"/>
Put the DataTemplate in the App.xaml: 将数据模板放入App.xaml中:
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<DataTemplate DataType="{x:Type localViewModel:HomeViewModel }">
<localView:AccueilView/>
</DataTemplate>
<DataTemplate DataType="{x:Type localViewModel:ErrorViewModel }">
<localView:ErrorView/>
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
Register the ViewModel with Simple IOC in the ViewModelLocator: 在ViewModelLocator中向简单IOC注册ViewModel:
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<IHomeViewModel, DesignHomeViewModel>();
}
else
{
SimpleIoc.Default.Register<IHomeViewModel, HomeViewModel>();
}
Set the getter of all the ViewModel in the ViewModelLocator to Static (just one for the exemple) 将ViewModelLocator中所有ViewModel的getter设置为Static(仅一个示例)
public static IHomeViewModel Home
{
get{return ServiceLocator.Current.GetInstance<IHomeViewModel>();}
}
Since it's static you can access the ViewModel you want from the MainViewModel: 由于它是静态的,因此您可以从MainViewModel中访问所需的ViewModel:
public class MainViewModel : ViewModelBase,IMainViewModel
{
public ViewModelBase HomeVM
{
get
{
return (ViewModelBase)ViewModelLocator.Home;
}
}
}
Provide the ability to unregister the ViewModel and recreates it: 提供注销ViewModel并重新创建它的功能:
public static void CleanUpHome()
{
SimpleIoc.Default.Unregister<HomeViewModel>();
SimpleIoc.Default.Register<IHomeViewModel, HomeViewModel>();
}
The "child" View Model communicates with the MainViewModel through messages: “子”视图模型通过以下消息与MainViewModel进行通信:
public class ErrorViewModel : ViewModelBase, IErrorViewModel
{
/*Other piece of code */
public void HomeReturn()
{
var msg = new ChangeView(ChangeView.EnumView.Home);
Messenger.Default.Send<ChangeView>(msg);
ViewModelLocator.CleanUpErrors();
}
}
The MainViewModel Register to the message and processes it: MainViewModel注册到消息并对其进行处理:
public class MainViewModel : ViewModelBase,IMainViewModel
{
public MainViewModel()
{
Messenger.Default.Register<ChangeView>(this, (action) => ReceiveMessage(action));
CurrentViewModel = HomeVM;
}
private void ReceiveMessage(ChangeView viewName)
{
switch (viewName.switchView)
{
case ChangeView.EnumView.Home:
CurrentViewModel = HomeVM;
break;
case ChangeView.EnumView.Error:
CurrentViewModel = ErrorVM;
break;
}
Messenger.Default.Unregister<ChangeView>(this, (action) => ReceiveMessage(action));
}
That's all. 就这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.