【问题标题】:Delegate command not executing on property change属性更改时未执行委托命令
【发布时间】:2016-06-09 18:11:21
【问题描述】:

我目前正在尝试使 ViewA 中的滑块更改 viewA 和 viewB 中文本的字体大小。我已经正确绑定了所有内容,但是当字体大小属性更改时,委托命令没有调用执行方法。如果我手动调用这个函数,一切都会按预期工作,所以很可能是一行代码出现了问题。 ViewAViewModel 如下:

public class ViewAViewModel : BindableBase
{
    private Person _CoolChick = new Person();
    private int _fontSize = 12;
    private IEventAggregator _eventAggregator;
    public DelegateCommand UpdateSizeCommand { get; set; }

    public Person CoolChick
    {
        get
        {
            return _CoolChick;
        }
        set
        {
            SetProperty(ref _CoolChick, value);
        }
    }

    public int FontSize
    {
        get { return _fontSize; }
        set {
            Console.WriteLine(_fontSize + " => Font Size");
            SetProperty(ref _fontSize, value);
            //Execute();
        }
    }

    public ViewAViewModel(IEventAggregator eventAggregator)
    {
        CoolChick.Age = 25;
        CoolChick.Name = "Methalous";
        _eventAggregator = eventAggregator;

        //likely the problem in this code
        UpdateSizeCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => FontSize);

    }

    private void Execute()
    {
        _eventAggregator.GetEvent<UpdateEvent>().Publish(FontSize);
    }

    private bool CanExecute()
    {
        return true;
    }
}

【问题讨论】:

  • 您的绑定可能不正确。使用像 Snoop 这样的工具在运行时检查您的绑定。

标签: mvvm prism delegatecommand


【解决方案1】:

为什么会这样?您没有在 Font 属性的设置器中调用 UpdateSizeCommand.Execute 。除非您将命令绑定到命令属性或手动调用它,否则该命令不会调用。

【讨论】:

  • 哦,我现在明白了。我希望在观察到的属性发生变化时调用 execute 方法。
猜你喜欢
  • 1970-01-01
  • 2012-03-25
  • 2017-07-07
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多