代码之家  ›  专栏  ›  技术社区  ›  Snake Eyes

使用Xamarin C创建可绑定属性#未提供任何属性,找到可绑定属性

  •  1
  • Snake Eyes  · 技术社区  · 6 年前

    我创建了一个派生自 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 ...} 不起作用。

    1 回复  |  直到 6 年前
        1
  •  7
  •   BasisPrune    6 年前

    根据Xamarin docs,以下是如何定义可绑定属性:

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

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties