使用MVVM LIGHT(WPF)浏览UserControl [英] Navigate through UserControl with MVVM LIGHT (WPF)

查看:297
本文介绍了使用MVVM LIGHT(WPF)浏览UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我为我的英语不佳而道歉,这不是我的母语.

First of all I apologize for my poor english which is not my first language.

我是MVVM的新手,所以我的问题可能是一个新手;)

I'm new in MVVM so my question is probably a very newbie one ;)

在使用WPF和MVVM LIGHT的C#应用​​程序中切换视图时遇到一些问题.我已经读了很多文章,但是我仍然不知道如何以一种干净的方式来做.

I'm encountering some issue with switching View in a C# Application using WPF and MVVM LIGHT. I've read a lot of articles but i still can't figured out how to do it in a clean way.

所以这是我的问题:假设在MainWindows中,实现UserControl之间的导航的最佳方法是什么?

So here is my question: What is the best way to achieve the navigation between UserControl contained in a MainWindows, assuming that:

  • 我为每个UserControl有一个ViewModel,为主Windows有一个ViewModel.
  • 用于在用户控件之间切换的按钮包含在UserControl本身中
  • 我有一个ViewModelLocator
  • 有时我需要销毁/重新创建userControl的ViewModel
  • 我想尊重MVVM模式.
  • 我想保持简单

推荐答案

由于没有人回答我的问题,所以我终于做到了. 这可能不是最好的方法,但至少效果很好. 我希望它会对像我这样努力学习这种模式的新手有所帮助:

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:

在MainViewModel中放置一个CurrentViewModel对象:

public class MainViewModel : ViewModelBase,IMainViewModel
{ 
    /* Other piece of code */

    private ViewModelBase _currentViewModel;

     public ViewModelBase CurrentViewModel
     {
         get
         {
             return _currentViewModel;
         }
         set
         {
             _currentViewModel = value;
             RaisePropertyChanged(() => CurrentViewModel);
         }
     }  
}

显然将此绑定到Mainview(只是相关代码):

<UserControl Content="{Binding Path=CurrentViewModel}"/>

将DataTemplate放入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>

在ViewModelLocator中向简单IOC注册ViewModel:

if (ViewModelBase.IsInDesignModeStatic)
{
    SimpleIoc.Default.Register<IHomeViewModel, DesignHomeViewModel>();
}
else
{
    SimpleIoc.Default.Register<IHomeViewModel, HomeViewModel>();
}

将ViewModelLocator中所有ViewModel的吸气剂设置为静态"(以一个为例)

public static IHomeViewModel Home
{
    get{return ServiceLocator.Current.GetInstance<IHomeViewModel>();}
}

由于它是静态的,因此可以从MainViewModel中访问所需的ViewModel:

public class MainViewModel : ViewModelBase,IMainViewModel
{
        public ViewModelBase HomeVM
        {
            get
            {
                return (ViewModelBase)ViewModelLocator.Home;
            }
        }
}

提供注销ViewModel并重新创建ViewModel的功能:

public static void CleanUpHome()
{
    SimpleIoc.Default.Unregister<HomeViewModel>();
    SimpleIoc.Default.Register<IHomeViewModel, HomeViewModel>();
}

子"视图模型通过消息与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();
        }
}

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));
    }

仅此而已.

这篇关于使用MVVM LIGHT(WPF)浏览UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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