簡體   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