Xamarin.forms bindableProperty无法获取值?

Xamarin.forms bindableProperty无法获取值?,xamarin.forms,Xamarin.forms,我在customview中创建了bindableproperty,但它无法获取值 代码如下: public string BackgroundImage { get { var image = (image)GetValue(BackgroundImageProperty); return image; } set =&g

我在customview中创建了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);
xaml代码:

<local:MyView BackgroundImage="back.png" />

我想给一个字符串值,然后给一个图像。
我在xaml中为背景图像提供了一个字符串值。

解决方案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中设置绑定上下文

public MyView
{
  //...
  BindingContext = this;
  //...
}

您可以在xaml中共享代码。这将帮助我们解决此问题。此外,请与我们共享您如何为该属性赋值,这将help@LucasZhang-MSFT我已经添加了它在你的项目中什么是
图像
?BackgroundImage将返回一个字符串。您在customview中设置了BindingContext吗?我设置了BindingContext我现在只想获取字符串值。您会更改答案吗?我会删除我的。当然,我会用更多细节来改进它。