作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 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/
我是一名优秀的程序员,十分优秀!