2

It may sound like a dumb question but why are bindable properties static?

public static readonly BindableProperty MapSpanProperty = BindableProperty.Create(
    propertyName: "MapSpan",
    returnType: typeof(MapSpan),
    declaringType: typeof(BindableMap),
    defaultValue: null,
    defaultBindingMode: BindingMode.TwoWay,
    propertyChanged: MapSpanPropertyChanged);

public MapSpan MapSpan
{
    get
    {
        return (MapSpan)GetValue(MapSpanProperty);
    }
    set
    {
        SetValue(MapSpanProperty, value);
    }
}

I have this code and it works just fine if I make the bindable property static, otherwise, it doesn't work. If I make this bindable property static it means, let's say if I have 2 maps opened at the same time, that this value will be the same on both if I set it on one of them?

Julian
  • 5,290
  • 1
  • 17
  • 40

2 Answers2

0

Each bindable property has a corresponding public static readonly field of type BindableProperty that is exposed on the same class and that is the identifier of the bindable property.

You could check the source code of the control which provide the binable property.

I use the contentview for example. The code is from the link below. Xamarin forms update viewmodel field from a bindable property

public partial class MyCustomControl : ContentView
{
    private string _text;
    public string Text
    {
        get { return _text; }
        set
        {
            _text = value;
            OnPropertyChanged();
        }
    }
    public static readonly BindableProperty TextProperty = BindableProperty.Create(
             nameof(Text),
             typeof(string),
             typeof(MyCustomControl),
             string.Empty,
             propertyChanged: (bindable, oldValue, newValue) =>
             {
                 var control = bindable as MyCustomControl;
                 //var changingFrom = oldValue as string;
                 //var changingTo = newValue as string;
                 control.Title.Text = newValue.ToString();
             });
    
    public MyCustomControl()
    {
        InitializeComponent();
    }

The ContentView provide the public static readonly BindableProperty.

  //
// Summary:
//     An element that contains a single child element.
//
// Remarks:
//     The following example shows how to construct a new ContentView with a Label inside.
[ContentProperty("Content")]
public class ContentView : TemplatedView
{
    //
    // Summary:
    //     Backing store for the Xamarin.Forms.ContentView.Content property..
    //
    // Remarks:
    //     To be added.
    public static readonly BindableProperty ContentProperty;

    public ContentView();

    //
    // Summary:
    //     Gets or sets the content of the ContentView.
    public View Content { get; set; }

    //
    // Summary:
    //     Method that is called when the binding context changes.
    //
    // Remarks:
    //     To be added.
    protected override void OnBindingContextChanged();
}

You could check the MS docs about more for the static in binable property. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
0

As far as I understand, it's static just for get describing data before moment of creation. But it's not being used as static. BindableObject base class define INSTANДE of Dictionary<BindablePropery, BindablePropertyContext>. BindablePropertyContext contains our value. It means that each our class has static BindablePropery, but every of them get it's own context from it's own dictionary.