【发布时间】:2019-05-16 08:31:53
【问题描述】:
我已经开始使用 WPF MVVM Light,现在我正在尝试在页面之间导航。
在主窗口中我添加了一个“BackButton”
<Button Command='{Binding Main.GoBack, Mode=OneWay}' />
绑定到 MainViewModel 方法“RelayCommand GoBack”。
private RelayCommand _goBack;
public RelayCommand GoBack
{
get
{
return _goBack
?? (_goBack = new RelayCommand(
() =>
_navigationService.GoBack();
}));
}
}
为什么这个按钮只改变一次视图?如果我想第二次点击它 它不起作用(什么也没发生)。如果我通过另一个按钮将页面更改为另一个页面,则它只会一次又一次地开始工作。
FrameNavigationService的部分实现:
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;
}
}
我能做些什么来处理这个问题?可能我应该做完全不同的事情吗?
【问题讨论】:
标签: c# wpf mvvm mvvm-light relaycommand