DelegateCommand в отдельном классе, первый вызов параметра CanExecute равен нулю - PullRequest
0 голосов
/ 05 июля 2018

У меня следующая проблема. Я переместил DelegateCommand из ViewModel в отдельный класс. И наблюдать свойство в ViewModel. Это работает до сих пор.

Тогда CanExecute вызовет первый с NULL, когда представление инициализируется. Что тоже правильно. Затем в первый раз вызывается OnNavigatedTo и устанавливается TestModel. Но затем CanExecute вызывается снова с NULL, что неправильно. Если OnNavigatedTo затем вызывается во второй раз, и TestModel установлен, значение передается в метод CanExecute правильно.

CommandClass:

public class CommandFactory : BindableBase, ICommandFactory
{
#region Fields

private ICommand buttonTestCommandLocal;

#endregion

#region Properties

public ICommand ButtonTestCommand 
{
  get { return buttonTestCommandLocal ?? (buttonTestCommandLocal = new DelegateCommand<ITestModel>(ButtonTestCommand_Executed, ButtonTestCommand_CanExecute)); }
}

#endregion

#region Methods

private bool ButtonTestCommand_CanExecute(ITestModel parameter)
{
  return parameter != null;
}

private void ButtonTestCommand_Executed(ITestModel parameter)
{
  int x = 30;

  Console.WriteLine(x.ToString());
}

#endregion
}

public interface ICommandFactory
{
  ICommand ButtonTestCommand { get; }
}

ViewModel:

public class ButtonRegionViewModel : BindableBase, INavigationAware
{
#region Fields

private ITestModel testModelLocal;

#endregion

#region Constructors and destructors

public ButtonRegionViewModel(ICommandFactory commandFactory)
{
  CommandFactory = commandFactory;

  //Does not work
  if(commandFactory.ButtonTestCommand is DelegateCommand<ITestModel> buttonTestCommand)
    buttonTestCommand.ObservesProperty(()=> TestModel);
}

#endregion

#region Properties

public ITestModel TestModel
{
  get => testModelLocal;
  private set
  {
    SetProperty(ref testModelLocal, value);
    //Does work
    //if (CommandFactory.ButtonCommand is IModelCommand modelCommand)
    //  modelCommand.RaiseCanExecuteChanged();
  }
}

#endregion

#region Methods

public bool IsNavigationTarget(NavigationContext navigationContext)
{
  return true;
}

public void OnNavigatedFrom(NavigationContext navigationContext)
{
}

public void OnNavigatedTo(NavigationContext navigationContext)
{
  if (!(navigationContext.Parameters["Element"] is ITestModel testModel))
    return;

  TestModel = testModel;
}

#endregion
}

Вид:

<Grid>
<Grid.RowDefinitions>
  <RowDefinition Height="*"/>
  <RowDefinition Height="Auto"/>
  <RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="1" Margin="2" Padding="0"  HorizontalAlignment="Center" HorizontalContentAlignment="Center" 
        CommandParameter="{Binding Path=TestModel, Mode=OneWay}" Content="CommandFactory.ButtonTestCommand"
        Command="{Binding Path=CommandFactory.ButtonTestCommand}" Width="200" Height="100"/>
</Grid>

Я понятия не имею, почему это не работает в первый раз. Поскольку RaiseCanExecuteChanged работает напрямую.

Спасибо за вашу помощь: -)

...