我的源代码:
class TMViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName]string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#region LiveViewers
private int _liveviewers = 0;
public int LiveViewers
{
get { return _liveviewers; }
set
{
if (value == this._liveviewers)
return;
this._liveviewers = value;
NotifyPropertyChanged();
}
}
#endregion
public ICommand Start
{
get
{
return new DelegateCommand((obj) =>
{
LiveViewers++;
MessageBox.Show($"LiveViewers: {LiveViewers} | _liveviewers: {this._liveviewers}");
});
}
}
}
DelegateCommand具有默认的execute方法(就像我认为的Relaycommand一样),并且可以正常工作。
然后,我单击“开始”按钮,我得到一个带有LiveViewers变量数据的消息框(每次增加1),但它不会更新我的UI(停留在0)。
我做错了什么?