我有一个关于 C# 新 MVVM 工具包的问题,
我想知道为什么这段代码有效;
string MyProp => inputSystemName;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(MyProp))]
private string inputSystemName;
partial void OnInputSystemNameChanged(string? oldValue, string newValue)
{
Debug.WriteLine($"InputSystemName changed from {oldValue} to {newValue}");
Debug.WriteLine($"MyProp is now {MyProp}");
}
但是这段代码并没有更新myProp的值:
private string myProp = String.Empty;
public string MyProp
{
get => myProp;
set => myProp = value;
}
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(MyProp))]
private string inputSystemName;
partial void OnInputSystemNameChanged(string? oldValue, string newValue)
{
Debug.WriteLine($"InputSystemName changed from {oldValue} to {newValue}");
Debug.WriteLine($"MyProp is now {MyProp}");
}
这可能是我缺乏对通知属性如何工作的理解,但它不是更新另一个属性吗?
谢谢!
它不会设置属性,只是通知它发生了变化。所以你需要这样的东西:
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(MyProp))]
private string inputSystemName;
partial void OnInputSystemNameChanged(string? oldValue, string newValue)
{
Debug.WriteLine($"InputSystemName changed from {oldValue} to {newValue}");
Debug.WriteLine($"MyProp is now {MyProp}");
MyProp = New value:
}