根據this instruction,我試圖使用INotifyPropertyChanged函數來刷新我的UWP應用程序中的綁定數據。不幸的是,當數據的值從代碼後面改變時,事件PropertyChangedEventHandler返回null值(PropertyChanged = null在檢查這個陳述期間:if(PropertyChanged!= null))。當值從應用程序頁面更改(通過輸入值到文本框)PropertyChanged設置了一些值。UWP INotifyPropertyChanged爲空(c#)
我的類INotifyPropertyChanged的:
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:
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));
}
}
在後面的代碼執行
<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) – kristof43
檢查是否有幫助。我有一個類似的問題:http://stackoverflow.com/questions/42354995/propertychanged-is-null-uwp – KonKarapas