MVVM Light 5.0:如何使用导航服务

MVVM Light 5.0:如何使用导航服务,第1张

MVVM Light 5.0:如何使用导航服务

是的,在其最新版本中进行了

MvvmLight
介绍,但是他们没有提供任何实现(您可以使用WP中的已实现,Metroapps等),但不幸的是,您需要自行实现,这里是我目前的做法它(信用)
NavigationService
Wpf
NavigationService``Wpf

首先 创建您的导航界面

MvvmLight
INavigationService

public interface IframeNavigationService : INavigationService{    object Parameter { get; }  }

所述

Parameter
用于传递之间的物体
ViewModels
,并且
INavigationService
是其一部分
GalaSoft.MvvmLight.Views
的命名空间

然后像这样实现该接口

    class frameNavigationService : IframeNavigationService,INotifyPropertyChanged    {        #region Fields        private readonly Dictionary<string, Uri> _pagesByKey;        private readonly List<string> _historic;        private string _currentPageKey;          #endregion        #region Properties          public string CurrentPageKey        { get {     return _currentPageKey; } private  set {     if (_currentPageKey == value)     {         return;     }     _currentPageKey = value;     onPropertyChanged("CurrentPageKey"); }        }        public object Parameter { get; private set; }        #endregion        #region Ctors and Methods        public frameNavigationService()        { _pagesByKey = new Dictionary<string, Uri>(); _historic = new List<string>();        }  public void GoBack()        { if (_historic.Count > 1) {     _historic.RemoveAt(_historic.Count - 1);     NavigateTo(_historic.Last(), null); }        }        public void NavigateTo(string pageKey)        { NavigateTo(pageKey, null);        }        public virtual void NavigateTo(string pageKey, object parameter)        { lock (_pagesByKey) {     if (!_pagesByKey.ContainsKey(pageKey))     {         throw new ArgumentException(string.Format("No such page: {0} ", pageKey), "pageKey");     }     var frame = GetDescendantFromName(Application.Current.MainWindow, "Mainframe") as frame;     if (frame != null)     {         frame.Source = _pagesByKey[pageKey];     }     Parameter = parameter;     _historic.Add(pageKey);     CurrentPageKey = pageKey; }        }        public void Configure(string key, Uri pageType)        { lock (_pagesByKey) {     if (_pagesByKey.ContainsKey(key))     {         _pagesByKey[key] = pageType;     }     else     {         _pagesByKey.Add(key, pageType);     } }        }        private static frameworkElement GetDescendantFromName(DependencyObject parent, string name)        { var count = VisualTreeHelper.GetChildrenCount(parent); if (count < 1) {     return null; } for (var i = 0; i < count; i++) {     var frameworkElement = VisualTreeHelper.GetChild(parent, i) as frameworkElement;     if (frameworkElement != null)     {         if (frameworkElement.Name == name)         {  return frameworkElement;         }         frameworkElement = GetDescendantFromName(frameworkElement, name);         if (frameworkElement != null)         {  return frameworkElement;         }     } } return null;        }        public event PropertyChangedEventHandler PropertyChanged;        protected virtual void onPropertyChanged([CallerMemberName] string propertyName = null)        { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));        }        #endregion    }

Mainframe
上面的代码是x:一个简单的名称
frame
中定义的控制
Xaml
用于导航页面之间(定制根据您的需求)

其次 :在中

viewmodellocator
,将导航服务
SetupNavigation()
)初始化,以便可以在视图模型中使用它:

static ViewModelLocator(){     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);     SetupNavigation();     SimpleIoc.Default.Register<MainViewModel>();     SimpleIoc.Default.Register<LoginViewModel>();     SimpleIoc.Default.Register<NoteViewModel>();  } private static void SetupNavigation() {     var navigationService = new frameNavigationService();     navigationService.Configure("LoginView", new Uri("../Views/LoginView.xaml",UriKind.Relative));     navigationService.Configure("Notes", new Uri("../Views/NotesView.xaml", UriKind.Relative));      SimpleIoc.Default.Register<IframeNavigationService>(() => navigationService); }

第三: 最终,使用服务,例如

 public LoginViewModel(IframeNavigationService navigationService) {      _navigationService = navigationService;  }..._navigationService.NavigateTo("Notes",data);..

编辑

在此仓库中可以找到一个明确的样本。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5567129.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存