xamarin.forms - bindableProperty 无法获取值?
问题描述
我在自定义视图中创建了一个可绑定属性,但它无法获得价值。
这是代码:
public string BackgroundImage
{
get
{
var image = (image)GetValue(BackgroundImageProperty);
return image;
}
set => SetValue(BackgroundImageProperty, value);
}
图像为空。
public static readonly BindableProperty BackgroundImageProperty = BindableProperty.Create(nameof(BackgroundImage), typeof(string), typeof(MyView), null);
xml代码:
<local:MyView BackgroundImage="back.png" />
我确实想提供一个字符串值并将其提供给图像。我在 xaml 中给 BackgroundImage 一个字符串值。
解决方案
解决方案1: 可以从事件propertyChanged中获取值
public static readonly BindableProperty TextProperty = BindableProperty.Create(
nameof(Text),
typeof(string),
typeof(MyView),
null,
propertyChanged: (bindable, oldValue, newValue) =>
{
var value = newValue;
// do some thing you want .
}
);
解决方案 2: 您可以 在 CustomView中设置BindingContext 。
public MyView
{
//...
BindingContext = this;
//...
}
推荐阅读
- ruby - 如何在帖子正文中包含图像数据和边界?
- python - 熊猫时间序列分类问题
- kotlin - 错误:找不到符号导入 com.gourav.news.databinding.ActivityDetailBindingImpl;
- firebase - 设置默认 Firebase 文档语言
- javascript - 如何删除ajax帖子上部分视图元素的名称前缀?
- python-3.x - Python Selenium Webdriver:没有这样的元素或元素不可见或 InvalidSelectorException
- c++ - 可变参数 std::tuple 的复制赋值运算符
- botframework - 使用 LuisDialog 向 QnA Maker 添加丰富的卡片
- python-3.x - 如何使用 google.cloud.speech_v1p1beta1 获取整个成绩单?
- python - 如何在目录中的每个文件上迭代函数