WPF MvvmLight简单实例(1) 页面导航
2021-04-01 14:24
标签:ast dict readonly ati .com 新建 soft read parent 实现了那些功能,先看看截图: 操作描述: 在程序运行后,点击“Load”按钮,页面会加载PageOne,点击PageOne页面中的“Next”按钮即可进入PageTwo页面, 点击PageTwo页面中的“Next”即可进入PageThree页面,点击Back可返回Page1页面 第一步:新建工程并使用NuGet安装MvvmLight 第二步:添加Views文件夹并添加相应的ViewModel 本文主要描述如何使用MvvmLight实现简单的导航效果,所以页面基本上都是大同小异比较简单ViewModel也比较简单,所以这里只PageOne.xaml以及PageOneViewModel.cs PageOne.xaml代码: PageOneViewModel.cs代码: 第三步:修改ViewModelLocator.cs NavigationService.cs代码: 源码下载地址:http://download.csdn.net/detail/qindongshou1/9481969 此文仅作学习中的记录,希望对看到此文的同学有一点点的帮助。 文中如果有不正确的地方欢迎指正。 WPF MvvmLight简单实例(1) 页面导航 标签:ast dict readonly ati .com 新建 soft read parent 原文地址:https://www.cnblogs.com/lonelyxmas/p/9236637.html public class PageOneViewModel : ViewModelBase
{
private INavigationService _navigationService;
public ICommand GoBackCommand { get; set; }
public ICommand GoToNextCommand { get; set; }
public ICommand ChangeCommand { get; set;}
private string _title;
public string Title
{
get { return _title; }
set
{
Set(()=>Title,ref _title,value);
}
}
public PageOneViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
Title = "Please Click me!";
GoBackCommand = new RelayCommand(GoBack);
GoToNextCommand = new RelayCommand(GotoNext);
ChangeCommand = new RelayCommand(ChangeTitle);
}
private void ChangeTitle()
{
Title = "Hello MvvmLight!!!";
}
private void GoBack()
{
_navigationService.GoBack();
}
private void GotoNext()
{
var navigationService = ServiceLocator.Current.GetInstance
public class ViewModelLocator
{
///
public class NavigationService : ViewModelBase,INavigationService
{
private readonly Dictionarystring, Uri> _pagesByKey;
private readonly Liststring> _historic;
private string _currentPageKey;
#region Properties
public string CurrentPageKey
{
get
{
return _currentPageKey;
}
private set
{
Set(()=>CurrentPageKey,ref _currentPageKey,value);
}
}
public object Parameter { get; private set; }
#endregion
#region Ctors and Methods
public NavigationService()
{
_pagesByKey = new Dictionarystring, Uri>();
_historic = new Liststring>();
}
public void GoBack()
{
if (_historic.Count > 1)
{
_historic.RemoveAt(_historic.Count - 1);
NavigateTo(_historic.Last(), "Back");
}
}
public void NavigateTo(string pageKey)
{
NavigateTo(pageKey, "Next");
}
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;
if (parameter.ToString().Equals("Next"))
{
_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 )
{
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;
}
#endregion
}
文章标题:WPF MvvmLight简单实例(1) 页面导航
文章链接:http://soscw.com/index.php/essay/70956.html