2017-06-07 21 views
3

Xamarin.Formsのxamlにカスタムコントロールがあり、タイプintのプロパティを追加したいと思います。私はBindableプロパティを使用する必要があると思うので、後でViewModelからプロパティをバインドできます。Xamarin.FormsでBindableProperty.Createを使用する方法?

私はthisトピックを見つけましたが、私はそれを使用するかどうかはわかりません...そこにある:

BindableProperty.Create(nameof(ItemsSource), typeof(IList), typeof(BindablePicker), null, 
    propertyChanged: OnItemsSourcePropertyChanged); 

「BindablePicker」何ですか?それはプロパティが宣言されているビューですか?

は、ここに私の試みです:

public int WedgeRating 
    { 
     get 
     { 
      return (int)GetValue(WedgeRatingProperty); 
     } 
     set 
     { 
      try 
      { 
       SetValue(WedgeRatingProperty, value); 
      } 
      catch (ArgumentException ex) 
      { 
       // We need to do something here to let the user know 
       // the value passed in failed databinding validation 
      } 
     } 
    } 

    public static readonly BindableProperty WedgeRatingProperty = 
     BindableProperty.Create(nameof(WedgeRating), typeof(int), typeof(GameCocosSharpView), null, propertyChanged: OnItemsSourcePropertyChanged); 

    private static void OnItemsSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue) 
    { 
    } 

私もXAMLでそれを使用していない、それはまだ動作しません。特別な例外はありません。カスタムコントロールが初期化されたページのみがうまくいきません。私がここにペーストした行をコメントすると、それは動作します。

答えて

2

コードは良好です。デフォルト値をnullから0またはdefault(int)に変更してください。あなたはnullとしていますが、intプロパティは決してnullにはなりません。これが「クラッシュ」の理由でした。

public static readonly BindableProperty WedgeRatingProperty = 
    BindableProperty.Create (nameof (WedgeRating), typeof (int), typeof (GameCocosSharpView), default(int), propertyChanged: OnItemsSourcePropertyChanged); 

これが役立ちます。ここで

2

は、バインド可能なプロパティ

public class GameCocosSharpView : View 
    { 
     public int WedgeRating 
     { 
      get { return (int)GetValue(WedgeRatingProperty); } 
      set { SetValue(WedgeRatingProperty, value); } 
     } 
     public static void WedgeRatingChanged(BindableObject bindable, object oldValue, object newValue) 
     { 

     } 
     public static readonly BindableProperty WedgeRatingProperty = 
      BindableProperty.Create("WedgeRating", typeof(int), typeof(GameCocosSharpView), 1, BindingMode.Default, null, WedgeRatingChanged); 

    } 
の例です
関連する問題