简体   繁体   English

使用MVVM LIGHT(WPF)浏览UserControl

[英]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之间导航的最佳方法是:

  • I've a ViewModel for each UserControl and one for the Main Windows. 我有一个ViewModel用于每个UserControl,一个用于主Windows。
  • The buttons for switching between usercontrols are contained into UserControl itself 用于在用户控件之间切换的按钮包含在UserControl自身中
  • I've a ViewModelLocator 我有一个ViewModelLocator
  • I need to sometimes Destroy/re-create a userControl's ViewModel 我有时需要销毁/重新创建userControl的ViewModel
  • I want to respect the MVVM Pattern. 我想尊重MVVM模式。
  • I want to keep it simple 我想保持简单

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.

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