zoukankan      html  css  js  c++  java
  • 在MVVMLight框架的ViewModel中实现NavigationService

    网上已经有很多方法了,比如通过Messenger来实现等等。这里我只讲述一种我比较喜欢的方法,因为它很方便

    首先定义一个ViewModel基类,将所有ViewModel子类继承这个基类。在基类中定义

            protected bool RemoveBackEntry { get; set; }
            public NavigationService NavigationService { get; set; }
            public NavigationContext NavigationContext { get; set; }
            public virtual void OnNavigatedTo(NavigationEventArgs e) { }
            public virtual void OnNavigatingFrom(NavigatingCancelEventArgs e) { }
            public virtual void OnNavigatedFrom(NavigationEventArgs e)
            {
                if (RemoveBackEntry)
                {
                    RemoveBackEntry = false;
                    NavigationService.RemoveBackEntry();
                }
            }

    因为在MVVM中,我们将会在所有页面上都加上DataContext,只要拿到DataContext就能拿到ViewModel,所以我们就可以定义子类WPhoneApplicationPage来继承PhoneApplicationPage这个类。

        public class WPhoneApplicationPage : PhoneApplicationPage
        {
            protected WPhoneApplicationPage()
            {
                Loaded += PageBaseLoaded;
            }
    
    
            private void PageBaseLoaded(object sender, RoutedEventArgs e)
            {
                var viewModel = DataContext as ViewModel.ViewModel;
                if (viewModel != null)
                {
                    viewModel.NavigationService = NavigationService;
                }
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
    
                var viewModel = DataContext as ViewModel.ViewModel;
                if (viewModel != null)
                {
                    viewModel.NavigationContext = NavigationContext;
                    viewModel.OnNavigatedTo(e);
                }
            }
    
            protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
            {
                base.OnNavigatingFrom(e);
                var viewModel = DataContext as ViewModel.ViewModel;
                if (viewModel != null)
                {
                    viewModel.NavigationContext = NavigationContext;
                    viewModel.OnNavigatingFrom(e);
                }
            }
    
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                base.OnNavigatedFrom(e);
                var viewModel = DataContext as ViewModel.ViewModel;
                if (viewModel != null)
                {
                    viewModel.NavigationContext = NavigationContext;
                    viewModel.OnNavigatedFrom(e);
                }
            }
        }

      这样在ViewModel中就可以调用NavigationService了。

      此外,还需要修改View,让View继承WPhoneApplicationPage

      xaml布局文件中

    1  <phone:WPhoneApplicationPage //修改PhoneApplicationPage 为 WPhoneApplicationPage
    2   xmlns:phone="clr-namespace:redrock.Core" //这里是你的WPhoneApplicationPage类所在的命名空间

      cs类文件中

    public partial class MainPage : WPhoneApplicationPage // 让MainPage继承WPhoneApplicationPage
    {
      public MainPage()
      {
        InitializeComponent();
      }
    }

      这样你可以在你的ViewModel中使用NavigationService以及重载 ViewModel基类中提供的导航方法

  • 相关阅读:
    高性能MySQL之锁详解
    采样方法-数据不均衡
    bert中的分词
    中文多分类 BERT
    Transformer+BERT+GPT+GPT2
    GPU下train 模型出现nan
    list 转换成dictionary,并统计词频
    python多个分割符split字符串
    tensorflow中的kernel/Adam 变量的来源
    tensorflow 使用预训练好的模型的一部分参数
  • 原文地址:https://www.cnblogs.com/toruneko/p/3278559.html
Copyright © 2011-2022 走看看