Я только что попытался обновить один из моих проектов WPF с MVVM Light 4.2.30 до 5.2. После этого я заметил, что мои RelayCommands
больше не запускают свои CanExecute
методы.
После быстрого поиска я нашел несколько статей, объясняющих проблему и предлагающих использовать пространство имен GalaSoft.MvvmLight.CommandWpf
вместо GalaSoft.MvvmLight.Command
. Однако я не могу найти пространство имен GalaSoft.MvvmLight.CommandWpf
. Когда я смотрю на GalaSoft.MvvMGalaSoft.MvvmLight.dll в «Обозревателе объектов» Visual Studio, я также не могу найти это пространство имен.
Как кажется, ни у кого другого, кроме меня, есть такая проблема - есть идеи, что я делаю неправильно?
Обновление:
Я создал небольшой пример проекта, который показывает, как я сейчас использую RelayCommands с их методами CanExecute в версии 4.2.30 MVVM light:
public class ViewModel : ViewModelBase
{
private bool _isReadOnly = false;
public ViewModel ()
{
this.DoSomethingCommand = new RelayCommand(DoSomething, CanDoSomething);
}
public bool IsReadOnly
{
get
{
return _isReadOnly;
}
set
{
_isReadOnly = value;
this.RaisePropertyChanged("IsReadOnly");
// With MVVMLight 4.2.30.23246 I did not need to call the RaiseCanExecuteChanged on any of my RelayCommands
// DoSomethingCommand.RaiseCanExecuteChanged();
}
}
public RelayCommand DoSomethingCommand { get; set; }
private bool CanDoSomething()
{
return !this.IsReadOnly;
}
private void DoSomething()
{
MessageBox.Show("Let's break the MVVM idea...");
}
}
Код XAML представления:
<Window x:Class="MVVMLight5.2CanExecuteTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVMLight5._2CanExecuteTest"
mc:Ignorable="d"
Title="Test" Height="150" Width="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0" Content="Is read only" IsChecked="{Binding IsReadOnly, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Row="1" Grid.Column="0" Content="Break me" Command="{Binding DoSomethingCommand}"/>
</Grid>
My target is that if I have a button in the View that uses the 'DoSomethingCommand' as a Command then this button should become disabled when my IsReadOnly property is turned to false. When using MVVM light 4.2.30 then this works without any additional so far but in MVVM light 5.2 I need to add the DoSomethingCommand.RaiseCanExecuteChanged(); to make the button go disabled in view.
Могу ли я как-то получить старое поведение с новой легкой структурой MVVM?
RelayCommand
находится в пространстве именCommandWpf
, только если вы ссылаетесь на PCL (переносимую) версию библиотеки. При нацеливании на .NET 4.0 и ниже он находится в пространстве именCommand
. Вы уверены, что виноват MvvmLight? Можете ли вы опубликовать любой код? - person Patrick Quirk   schedule 10.11.2015CanExecute
? У меня нет никаких проблем с этим, и я не знаю о каких-либо текущих ошибках, когдаCanExecute
не вызывается. Либо так, либо создайте SSCCE, в котором обнаружена ошибка. - person Patrick Quirk   schedule 16.11.2015