wpf开发中使用mvvmlight navigation-service
新项目需要使用mvvmlight wpf完成,需要做到在多个view间跳转。
使用简单的消息实现了这个功能,但是遇到一个问题:MainView中显示FuncView1,第一次没有问题,但是FuncView1隐藏后(或者关闭),再一次显示它,上面的控件都不再可用,没有找到问题根源,所以无法解决,
现在想通过NavigationService来实现跳转,但坑爹的是mvvmlight中定义了INavigation, 但没有针对wpf的实现,只有自己来了。
first create you navigation interface that Implements the MvvmLight INavigationService
public interface IFrameNavigationService:INavigationService { object Parameter { get; } }
the Parameter is used to pass objects between ViewModels, and the INavigationService is part of GalaSoft.MvvmLight.Views namespace
then implemente that interface like so
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