Tôi chỉ mới bắt đầu sử dụng lệnh với MVVM trong một ứng dụng. Tôi đã tìm thấy một số ví dụ và đã thử cả hai cách trong mã của tôi. Một số ví dụ có lệnh ràng buộc trong XAML như vậy:Lệnh Binding trong XAML vs Thuộc tính ICommand trong ViewModel
<CommandBinding Command="local:MainWindow.OpenRecentFile"
Executed="{Binding OpenRecentFile_Executed}" />
...
<MenuItem Header="{x:Static culture:TextResource.RecentFilesMenuItem}"
Command="local:MainWindow.RecentFilesCommand" >
Với OpenRecentFile_Executed là một phương pháp trong ViewModel và một ICommand tĩnh như vậy:
public static readonly ICommand OpenRecentFile =
new RoutedCommand("Open Recent", typeof(MainWindow));
Tôi cũng đã thấy, nơi có một tài sản trên ViewModel đó là loại ICommand đó là ràng buộc để trong Xem như vậy:
<MenuItem Header="Close Current File"
Command="{Binding CloseCurrentFileCommand}"
CommandParameter="{TemplateBinding DataContext}"/>
và trong ViewModel:
private ICommand closeCurrentFileCommand;
public ICommand CloseCurrentFileCommand
{
get
{
if (closeCurrentFileCommand == null)
{
closeCurrentFileCommand =
new RelayCommand(param => this.CloseCurrentCedarFile(param));
}
return closeCurrentFileCommand;
}
}
Lợi ích/hạn chế đối với mỗi phương pháp là gì?