Mvvm 属性更改时未执行委托命令
我目前正在尝试在ViewA中使用滑块来更改ViewA和viewB中文本的字体大小。我已正确绑定了所有内容,但在更改font-size属性时,委托命令没有调用execute方法。如果我手动调用这个函数,那么一切都会按预期工作,因此问题可能出在一行代码上。ViewAViewModel如下所示:Mvvm 属性更改时未执行委托命令,mvvm,prism,delegatecommand,Mvvm,Prism,Delegatecommand,我目前正在尝试在ViewA中使用滑块来更改ViewA和viewB中文本的字体大小。我已正确绑定了所有内容,但在更改font-size属性时,委托命令没有调用execute方法。如果我手动调用这个函数,那么一切都会按预期工作,因此问题可能出在一行代码上。ViewAViewModel如下所示: public class ViewAViewModel : BindableBase { private Person _CoolChick = new Person(); private i
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;
}
}
公共类ViewAViewModel:BindableBase
{
私人_CoolChick=新人();
私有整数_fontSize=12;
私人事件聚合器;
公共DelegateCommand UpdateSizeCommand{get;set;}
公众人物
{
得到
{
返回(CoolChick);;
}
设置
{
SetProperty(参考,值);
}
}
公共整数字体大小
{
获取{return\fontSize;}
设置{
Console.WriteLine(_fontSize+“=>字体大小”);
SetProperty(参考字体大小、值);
//执行();
}
}
公共视图视图模型(IEventAggregator事件聚合器)
{
年龄=25岁;
CoolChick.Name=“甲烷”;
_eventAggregator=eventAggregator;
//可能是这段代码中的问题
UpdateSizeCommand=新的DelegateCommand(执行,CanExecute)。ObservesProperty(()=>FontSize);
}
私有void Execute()
{
_eventAggregator.GetEvent().Publish(FontSize);
}
私人布尔CanExecute()
{
返回true;
}
}
为什么会这样?您没有调用UpdateSizeCommand。请在字体属性的setter中执行。除非您将命令绑定到命令属性或手动调用它,否则该命令将不会调用。为什么?您没有调用UpdateSizeCommand。请在字体属性的setter中执行。除非将命令绑定到命令属性或手动调用,否则不会调用该命令。您的绑定可能不正确。使用Snoop之类的工具在运行时检查绑定。您的绑定可能不正确。使用Snoop之类的工具在运行时检查绑定。哦,我明白了。我希望在观察到的属性更改时随时调用execute方法。哦,我现在明白了。我希望在观察到的属性更改时随时调用execute方法。