UWP C# - 为什么 NotifyPropertyChanged 在这种情况下不触发?
UWP C# - Why does NotifyPropertyChanged not fire in this scenario?
在以下情况下,NotifyPropertyChanged 不会触发,因此我的 UI 不会更新(显示的简化视图模型):
public class NetworkGraphViewModel : INotifyPropertyChanged
{
private String byteSentSpeed { get; set; }
public String ByteSentSpeed { get { return byteSentSpeed; } set { byteSentSpeed = value; NotifyPropertyChanged("ByteSentSpeed"); } }
private void MyEvent(object sender, object eventArgs)
{
byteSentSpeed = byteSentSpeed + 5;
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML:
<TextBlock Margin="30,5,10,5" Grid.Column="1" Grid.Row="2" x:Name="bytesSentSpeedBlock" Text="{Binding ByteSentSpeed}"/>
...而以下示例会更新 UI:
public class NetworkGraphViewModel : INotifyPropertyChanged
{
private String byteSentSpeed { get; set; }
public String ByteSentSpeed { get { return byteSentSpeed; } set { byteSentSpeed = value; NotifyPropertyChanged("ByteSentSpeed"); } }
private void MyEvent(object sender, object eventArgs)
{
byteSentSpeed = byteSentSpeed + 5;
NotifyPropertyChanged("ByteSentSpeed")
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML:
<TextBlock Margin="30,5,10,5" Grid.Column="1" Grid.Row="2" x:Name="bytesSentSpeedBlock" Text="{Binding ByteSentSpeed}"/>
也许我错误地假设更改会通过分配给 "byteSentSpeed" 的值传播到 public 变量 "ByteSentSpeed"?
这真的是最有效的方法吗,还是我在做一些愚蠢的事情?
byteSentSpeed
和 ByteSentSpeed
是两个不同的属性。设置前者不会调用引发 PropertyChanged
事件的后者的 setter。
并且byteSentSpeed
应该是支持字段而不是属性:
private String byteSentSpeed;
public String ByteSentSpeed { get { return byteSentSpeed; } set { byteSentSpeed = value; NotifyPropertyChanged("ByteSentSpeed"); } }
只要您想更新 UI:
,您应该设置 属性
private void MyEvent(object sender, object eventArgs)
{
ByteSentSpeed = ByteSentSpeed + 5;
}
在以下情况下,NotifyPropertyChanged 不会触发,因此我的 UI 不会更新(显示的简化视图模型):
public class NetworkGraphViewModel : INotifyPropertyChanged
{
private String byteSentSpeed { get; set; }
public String ByteSentSpeed { get { return byteSentSpeed; } set { byteSentSpeed = value; NotifyPropertyChanged("ByteSentSpeed"); } }
private void MyEvent(object sender, object eventArgs)
{
byteSentSpeed = byteSentSpeed + 5;
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML:
<TextBlock Margin="30,5,10,5" Grid.Column="1" Grid.Row="2" x:Name="bytesSentSpeedBlock" Text="{Binding ByteSentSpeed}"/>
...而以下示例会更新 UI:
public class NetworkGraphViewModel : INotifyPropertyChanged
{
private String byteSentSpeed { get; set; }
public String ByteSentSpeed { get { return byteSentSpeed; } set { byteSentSpeed = value; NotifyPropertyChanged("ByteSentSpeed"); } }
private void MyEvent(object sender, object eventArgs)
{
byteSentSpeed = byteSentSpeed + 5;
NotifyPropertyChanged("ByteSentSpeed")
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML:
<TextBlock Margin="30,5,10,5" Grid.Column="1" Grid.Row="2" x:Name="bytesSentSpeedBlock" Text="{Binding ByteSentSpeed}"/>
也许我错误地假设更改会通过分配给 "byteSentSpeed" 的值传播到 public 变量 "ByteSentSpeed"?
这真的是最有效的方法吗,还是我在做一些愚蠢的事情?
byteSentSpeed
和 ByteSentSpeed
是两个不同的属性。设置前者不会调用引发 PropertyChanged
事件的后者的 setter。
并且byteSentSpeed
应该是支持字段而不是属性:
private String byteSentSpeed;
public String ByteSentSpeed { get { return byteSentSpeed; } set { byteSentSpeed = value; NotifyPropertyChanged("ByteSentSpeed"); } }
只要您想更新 UI:
,您应该设置 属性private void MyEvent(object sender, object eventArgs)
{
ByteSentSpeed = ByteSentSpeed + 5;
}