繁体   English   中英

使用Xamarin C#创建可绑定属性没有给出任何属性,未找到可绑定属性

[英]Creating bindable property with Xamarin C# gave No property, bindable property found

我创建了一个从StackLayout派生的类:

public class MyStackLayout : StackLayout
{
    private readonly BindableProperty TriggerTypeProperty = BindableProperty.Create("TriggerType", typeof(string), typeof(MyStackLayout), defaultValue: "test", propertyChanged: (b, o, n) => OnTriggerTypePropChanged(b, (string)o, (string)n));

    public string TriggerType
    {
      get
      {
        return (string)GetValue(TriggerTypeProperty);
      }
      set
      {
        SetValue(TriggerTypeProperty, value);
      }
    }

    private static void OnTriggerTypePropChanged(BindableObject bindable, string oldvalue, string newvalue)
    {
      var obj = bindable as MyStackLayout;
      if (obj == null)
      {
        return;
      }

      obj.TriggerType = newvalue;
    }
}

在XAML中:

<controls1:MyStackLayout IsVisible="False" TriggerType="{Binding AStringPropertyFromViewModel}">
   ...
</controls1:MyStackLayout>

我收到错误消息:

找不到“ TriggerType”的属性,可绑定属性或事件,或值和属性之间的类型不匹配。

怎么了 ?

如果我在XAML中使用TriggerType="A string"可以正常工作,如果我使用{Binding ...}将不起作用。

根据Xamarin文档,这是如何定义可绑定属性:

public static readonly BindableProperty EventNameProperty =
BindableProperty.Create ("EventName", typeof(string),typeof(EventToCommandBehavior), null);

尝试将可绑定属性从私有更改为公共,以查看它是否解决了您的问题。

文档的链接: https : //docs.microsoft.com/zh-cn/xamarin/xamarin-forms/xaml/bindable-properties

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM