假设我正在使用 MVVM 方法(Silverlight)
我正在用命令处理我的所有按钮。
假设我有一个用于导航到某个页面的按钮,假设我们在网格中选择了一个客户并想要导航到客户的详细信息视图。
我可以用 DelegateCommand 处理这个按钮吗?如何?我可以处理来自 ViewModel 的导航吗?我是否被迫从代码隐藏处理导航。
假设我正在使用 MVVM 方法(Silverlight)
我正在用命令处理我的所有按钮。
假设我有一个用于导航到某个页面的按钮,假设我们在网格中选择了一个客户并想要导航到客户的详细信息视图。
我可以用 DelegateCommand 处理这个按钮吗?如何?我可以处理来自 ViewModel 的导航吗?我是否被迫从代码隐藏处理导航。
开始了:
xml:
<Button Command="{Binding GoToCustomerDetailsPage}" Content="Customer Details"/>
视图模型:
private INavigationService _navigationService;
public ViewModel(INavigationService navigationService)
{
_navigationService=navigationService;
}
public ICommand GoToCustomerDetailsPage
{
get{
return new DelegateCommand(GoToCustDetailsPage,CanGoToCustDetailsPage);
}
}
private void GoToCustDetailsPage()
{
_navigationService.Navigate(new Uri("/CustomerDetails", UriKind.Relative));
}
private bool CanGoToCustDetailspage()
{
return true;//Or some sensible code that determines if this is sensible.
}
基本上,命令按正常方式绑定到按钮。该命令是视图模型上的一个属性。执行命令时,它只需调用视图模型中的私有方法。
这里 INavigationService 不可用。如果我们添加命名空间 System.Windows.Navigation 会出错。