更改属性值后,为什么属性更改值为null?

时间:2015-11-25 09:51:06

标签: c# xaml xamarin.forms

任何人都可以告诉我PropertyChanged为什么INotifyPropertyChanged值在NotifyPropertyChanged中为空。我有一个属性,我更改了它的值,但是当它进入 public class WorkOrder : INotifyPropertyChanged { public string _orderDays="0 Days"; public string OrderDays { get { return _orderDays; } set { if (_orderDays == value) return; _orderDays = value; NotifyPropertyChanged("OrderDays"); } } public event PropertyChangedEventHandler PropertyChanged; // This method is called by the Set accessor of each property. // The CallerMemberName attribute that is applied to the optional propertyName // parameter causes the property name of the caller to be substituted as an argument. public void NotifyPropertyChanged([CallerMemberName]String propertyName =null ) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } 方法时,PropertyChanged的值为null。以下是我的代码:

<Label x:Name="lblTicketDate" Text="{Binding OrderDays}" 
    Grid.Row="1" HorizontalOptions="CenterAndExpand"
    VerticalOptions="EndAndExpand"  /> 

这是我绑定值

的xaml
 sed 's/_[^ ]\+//' FileName

1 个答案:

答案 0 :(得分:2)

  

但是当它进入NotifyPropertyChangd方法时,PropertyChanged的值为null

这只是意味着还没有订阅这个事件。除了事件之外,委托实例引用了在调用委托时要执行的一个或多个动作 - 空引用用于表示“0动作”。

如果您使用:

var order = new WorkOrder();
// Or whatever's useful instead of Console.WriteLine
order.PropertyChanged += (sender, args) => Console.WriteLine(e.PropertyName);
order.OrderDays = "Different";

然后你会看到事件被提出。