在此先感謝!WPF PRISM 6 DelegateComand ObservesCanExecute
我應該如何在PRISM 6的DelegateCommand中使用ObservesCanExecute?
public partial class UserAccountsViewModel: INotifyPropertyChanged
{
public DelegateCommand InsertCommand { get; private set; }
public DelegateCommand UpdateCommand { get; private set; }
public DelegateCommand DeleteCommand { get; private set; }
public UserAccount SelectedUserAccount
{
get;
set
{
//notify property changed stuff
}
}
public UserAccountsViewModel()
{
InitCommands();
}
private void InitCommands()
{
InsertCommand = new DelegateCommand(Insert, CanInsert);
UpdateCommand = new DelegateCommand(Update,CanUpdate).ObservesCanExecute(); // ???
DeleteCommand = new DelegateCommand(Delete,CanDelete);
}
//----------------------------------------------------------
private void Update()
{
//...
}
private bool CanUpdate()
{
return SelectedUserAccount != null;
}
//.....
}
不幸的是,我不熟悉c#中的表達式。另外,我認爲這對其他人有幫助。
有什麼理由不在這裏使用lambda?我能想到的唯一的事情是有一些緩存的resolves表達式。 – Gusdor
@Gusdor,正如alphe所說,這是因爲ObserveCanExecute鬆散地取代了[CanExecute](https://msdn.microsoft.com/en-us/library/ff654646.aspx?cs-save-lang=1&cs-lang=csharp #code-snippet-1)布爾方法;因此,它必須返回一個布爾值。 –