【发布时间】:2017-08-06 17:31:26
【问题描述】:
根据this instruction,我正在尝试使用 INotifyPropertyChanged 函数来刷新我的 UWP 应用程序中的绑定数据。不幸的是,当数据的值从后面的代码中更改时,事件 PropertyChangedEventHandler 返回 null 值(PropertyChanged = null 在检查此语句期间:if (PropertyChanged != null))。当从应用程序页面更改值时(通过向 TextBox 输入值)PropertyChanged 设置为某个值。
我的 INotifyPropertyChanged 课程:
public class UserOperation : INotifyPropertyChanged
{
private int _beginDistance, _endDistance;
public int BeginDistance {get { return _beginDistance; }
set
{
_beginDistance = value;
NotifyPropertyChanged("BeginDistance");
}
}
public int EndDistance { get { return _endDistance; }
set
{
_endDistance = value;
NotifyPropertyChanged("EndDistance");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
在后面的代码中实现:
public sealed partial class PartCreatePage : Page
{
private UserOperation operation { get; set; }
public PartCreatePage()
{
this.InitializeComponent();
operation = new UserOperation();
}
private void OperationAck_Button(object sender, RoutedEventArgs e)
{
operation.BeginDistance = 500;
}
还有xaml:
<TextBox x:Name="begin_Distance"
Text="{x:Bind operation.BeginDistance, Mode=TwoWay}"
KeyDown="onlyNumeric_KeyDown"
Style="{StaticResource OperationFlyout_TextBox}" />
【问题讨论】:
-
“事件PropertyChangedEventHandler返回空值”是什么意思?
-
这意味着:PropertyChanged = null 在检查此语句期间:if (PropertyChanged != null)
-
检查是否有帮助。我有类似的问题:stackoverflow.com/questions/42354995/…
-
同意 Tóth Tibor。看来代码应该是有效的。如果通过将值设置为 operation.BeginDistance 来更改数据,PropertyChanged 将不会为空。
标签: c# null uwp inotifypropertychanged