2017-04-08 100 views
1

我一直在通過Ed Snider的書,掌握Xamarin.Forms。指示創建從EntryCell繼承的類DatePickerEmtryCell。 它顯示添加以下DateTime BindableProperty,但現在不推薦使用此方法並生成一個錯誤。Xamarin.Forms自定義日期時間BindableProperty BindingPropertyChangedDelegate

public static readonly BindableProperty DateProperty = BindableProperty.Create<DatePickerEntryCell, DateTime>(p => 
    p.Date, 
    DateTime.Now, 
    propertyChanged: new BindableProperty.BindingPropertyChangedDelegate<DateTime>(DatePropertyChanged)); 

我認爲我是正確的軌道下面,但我不知道如何完成它,我完全被卡住:

public static readonly BindableProperty DateProperty = 
    BindableProperty.Create(nameof(Date), typeof(DateTime), typeof(DatePickerEntryCell), default(DateTime), 
     BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(

我認爲,這將是這個

new BindableProperty.BindingPropertyChangedDelegate(DatePickerEntryCell.DatePropertyChanged), null, null);  

但這是不正確的,以及我試過的無數其他排列。 我會喜歡一些指令。

乾杯

回答

2

由於DateProperty是靜態的,propertyChanged代表應該是靜態的爲好。由於它是BindingPropertyChangedDelegate類型。你可以試試這樣說:現在

public static readonly BindableProperty DateProperty = BindableProperty.Create(
     propertyName: nameof(Date), 
     returnType: typeof(DateTime), 
     declaringType: typeof(DatePickerEntryCell), 
     defaultValue: default(DateTime), 
     defaultBindingMode: BindingMode.TwoWay, 
     validateValue: null, 
     propertyChanged: OnDatePropertyChanged); 

,從委託,你應該有機會獲得代表您DatePickerEntryCell元素BindableObject。您還可以訪問舊/新值。以下是如何從代表檢索控制:

public static void OnDatePropertyChanged(BindableObject bindable, object oldValue, object newValue) 
{ 
    var control = bindable as DatePickerEntryCell; 
    if (control != null){ 
     // do something with this control... 
    } 
} 

希望它有幫助!

+0

謝謝這麼多,它的工作原理, – user1667474