Sarcel
Sarcel

Reputation: 27

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.

Upvotes: 1

Views: 614

Answers (1)

Sarcel
Sarcel

Reputation: 27

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 !

Upvotes: 1

Related Questions