gpt4 book ai didi

c# - 如何在 Xamarin.Forms 中使用 BindableProperty.Create?

转载 作者:太空狗 更新时间:2023-10-29 22:21:26 32 4
gpt4 key购买 nike

在 Xamarin.Forms 的 xaml 中,我有一个自定义控件,我想添加 int 类型的属性。我想我必须使用 Bindable 属性,所以稍后我可以从 ViewModel 绑定(bind)一个属性。

我找到了 this主题,但我不确定如何使用它。有:

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

什么是“BindablePicker”?是声明属性(property)的观点吗?

这是我的尝试:

    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 中使用它,它已经不起作用了。没有特别的异常(exception)。只有初始化自定义控件的页面不会加载。当我评论我粘贴在这里的行时,它起作用了。

最佳答案

您的代码很好,只需将您的默认值从 null 更改为 0 或 default(int)。您将其设置为 null,但 int 属性永远不能为 null。这就是“崩溃”的原因。

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

希望这对您有所帮助!

关于c# - 如何在 Xamarin.Forms 中使用 BindableProperty.Create?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44403136/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com