简体   繁体   中英

How to create binding property in Xamarin.Forms?

I have one drawing object SkiaView. I create many SkiaView in xaml file so I am looking for a way to passing custom property to my SkiaView.

I tried this:

(Author : 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); }
    }

But this returns string "none", I would like get and use my TestVar set in xaml. So if anybody have an idea for passing property to an object without Bindable Property I take it.

Thanks for your reply, i have change the code below like this

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); }
    }

And it's working !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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