Forum General

Change BindableProperty.Create() to BindableProperty.Create()

Emixam23Emixam23 USMember ✭✭✭
edited May 2016 in General

Hi,

It says that BindableProperty.Create<>() is deprecated and it will be removed soon.
So, since 3 weeks ago I search a solution and post many questions, but no one seems to have the answer..

I have this following code

    public static readonly BindableProperty RouteCoordinatesProperty =
    BindableProperty.Create<CustomMap, List<Position>>(
        p => p.RouteCoordinates, new List<Position>());

I'm using Xamarin.Forms.BindableProperty.Create<TDeclarer,TPropertyType> Method
And I want to change to Xamarin.Forms.BindableProperty.Create Method which is not deprecated

The problem is that I don't know how to organize the parameters...

public class CustomMap : Map
{
    public static readonly BindableProperty RouteCoordinatesProperty =
    BindableProperty.Create<CustomMap, List<Position>>(
        p => p.RouteCoordinates, new List<Position>());

    public List<Position> RouteCoordinates
    {
        get { return (List<Position>)GetValue(RouteCoordinatesProperty); }
        set { SetValue(RouteCoordinatesProperty, value); }
    }

    public CustomMap()
    {
        RouteCoordinates = new List<Position>();
    }
}

Best Answer

Answers

  • Emixam23Emixam23 USMember ✭✭✭

    Ho god... my maps finally works oO I red the documentation so much.. You saved my life ^^ thank !

  • KimJensenKimJensen USMember ✭✭

    I looked at above solution, but I can't find a way to change this.

    public static BindableProperty itemsSourceProperty =
    BindableProperty.Create<BindablePicker, IList>(o => o.ItemsSource, default(IList), propertyChanged: OnItemSourceChanged);

    I tried this but i get an error on OnItemSourceChanged

    public static BindableProperty itemsSourceProperty =
    BindableProperty.Create(nameof(ItemsSource), typeof(IList), typeof(BindablePicker), propertyChanged: OnItemSourceChanged);

    Thanks for any help.

  • Emixam23Emixam23 USMember ✭✭✭
    edited August 2017

    public static BindableProperty itemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IList), typeof(BindablePicker), /*Maybe you need a default value just here*/, propertyChanged: OnItemSourceChanged);

    But BindablePicked is weird.. Usually, it's the name of the page :/ let me show you my idea about your code.

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentPage, INotifyPropertyChanged
    {
        public static readonly BindableProperty DataProperty =
            BindableProperty.Create(nameof(Data), typeof(IList), typeof(MainPage), null, propertyChanged: OnItemSourceChanged);
    
        public IList Data
        {
            get { return (IList)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }
    
        public static void OnItemSourceChanged(BindableObject bindable, object oldValue, object newValue)
        {
            ((MainPage)bindable).OnItemSourceChanged((IList<string>)oldValue, (IList<string>)newValue);
        }
    
        public void OnItemSourceChanged(IList<string> oldValue, IList<string> newValue)
        {
            // Do your stuff in your object's/page's instance
        }
    }
    

    Did I answered your question or am I totally out of the subject?

  • KimJensenKimJensen USMember ✭✭
    edited August 2017

    Here is the whole class.
    Thanks for your help.

     [AddINotifyPropertyChangedInterface]
        public partial class BindablePicker : Picker
        {
            public BindablePicker()
            {
                this.SelectedIndexChanged += OnSelectedIndexChanged;
                //this.SelectedIndex = 0;
            }
    
            private static BindableProperty itemsSourceProperty =
                BindableProperty.Create<BindablePicker, IList>(o => o.ItemsSource, default(IList), propertyChanged: OnItemsSourceChanged);
    
            public static BindableProperty SelectedItemProperty =
                BindableProperty.Create<BindablePicker, object>(o => o.SelectedItem, default(object), propertyChanged: UpdateSelected);
    
    
            public string DisplayMember { get; set; }
    
            public float TextSize { get; set; }
            public bool HasIcon { get; set; } = true;
    
            public IList ItemsSource
            {
                get { return (IList)GetValue(ItemsSourceProperty); }
                set { SetValue(ItemsSourceProperty, value); }
            }
    
            public object SelectedItem
            {
                get { return (object)GetValue(SelectedItemProperty); }
                set { SetValue(SelectedItemProperty, value); }
            }
    
            public static BindableProperty ItemsSourceProperty { get { return ItemsSourceProperty1; } set { ItemsSourceProperty1 = value; } }
    
            public static BindableProperty ItemsSourceProperty1 { get => itemsSourceProperty; set => itemsSourceProperty = value; }
    
            private static void OnItemsSourceChanged(BindableObject bindable, IList oldvalue, IList newvalue)
            {
                var picker = bindable as BindablePicker;
    
                if (picker != null)
                {
                    if (picker.Items.Count > 0)
                        picker.Items.Clear();
    
                    if (newvalue == null) return;
    
                    foreach (var item in newvalue)
                    {
                        if (string.IsNullOrEmpty(picker.DisplayMember))
                        {
                            picker.Items.Add(item.ToString());
                        }
                        else
                        {
                            var prop = item.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayMember, StringComparison.OrdinalIgnoreCase));
                            if (prop != null)
                            {
                                picker.Items.Add(prop.GetValue(item).ToString());
                            }
    
                        }
                    }
                    picker.SelectedIndex = 0;
    
                }
            }
    
            protected override void OnPropertyChanged(string propertyName)
            {
                base.OnPropertyChanged(propertyName);
    
            }
    
    
            private void OnSelectedIndexChanged(object sender, EventArgs eventArgs)
            {
                if (SelectedIndex < 0 || SelectedIndex > Items.Count - 1)
                {
                    SelectedItem = -1;
                }
                else
                {
                    SelectedItem = ItemsSource[SelectedIndex];
                }
            }
    
            private static void UpdateSelected(BindableObject bindable, object oldvalue, object newvalue)
            {
    
                var picker = bindable as BindablePicker;
                if (picker != null)
                    if (picker.ItemsSource != null && picker.SelectedItem != null)
                        if (picker.ItemsSource.Contains(picker.SelectedItem))
                            picker.SelectedIndex = picker.ItemsSource.IndexOf(picker.SelectedItem);
                        else
                            picker.SelectedIndex = -1;
            }
    
    
    
        }
    
  • Emixam23Emixam23 USMember ✭✭✭

    I'm a bit busy now but I'll try to give you an answer soon

Sign In or Register to comment.