【问题标题】:How to create binding property in Xamarin.Forms?如何在 Xamarin.Forms 中创建绑定属性?
【发布时间】:2016-10-17 14:32:14
【问题描述】:

我有一个绘图对象 SkiaView。 我在 xaml 文件中创建了许多 SkiaView,因此我正在寻找一种将自定义属性传递给我的 SkiaView 的方法。

我试过这个:

(作者:https://codemilltech.com/back-to-school-adding-a-custom-bindable-property-to-xamarin-forms/


      public static readonly BindableProperty TestVarProperty =
        BindableProperty.Create<SkiaView, string>(rv => rv.TestVar,"None", 
            BindingMode.TwoWay, (bindable,value)=> { return true; },
            (bindable, oldValue, newValue) => {

                var thisView = (SkiaView)bindable;},
    (bindable, oldValue, newValue) => {},
    (bindable, value) => {
        return value;
    }, (bindable) => {
        return ">Error<";
    });

    public string TestVar
    {
        get { return (string)GetValue(TestVarProperty); }
        set { SetValue(TestVarProperty, value); }
    }

但这会返回字符串“none”,我想获取并使用我在 xaml 中设置的 TestVar。 因此,如果有人想将属性传递给没有可绑定属性的对象,我会接受。

【问题讨论】:

  • 您能否发布代码以显示您分配给TestVar 的内容?
  • 如果您找到了问题的答案,您应该将其作为答案发布,而不是将其编辑到问题中。

标签: c# visual-studio xaml xamarin xamarin.android


【解决方案1】:

感谢您的回复,我已将下面的代码更改为这样

public static readonly BindableProperty TestVarProperty =
   BindableProperty.Create<SkiaView, string>(rv => rv.TestVar, null,
       BindingMode.OneWayToSource);

    public string TestVar
    {
        get { return (string)GetValue(TestVarProperty); }
        set { SetValue(TestVarProperty, value); }
    }

它正在工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多