Looking for a better way to SetBinding Property.

WinterCloudWinterCloud GBMember ✭✭✭

SetBinding by expression is obsolete, this move create huge problems for me. How to bind complex data model now?

Easy Model:
Obsolete:
view.SetBinding<Model>(View.Text, x=>x.Text);
New way:
view.SetBinding(View.Text, nameof(Model.Text));

Complex Model:
Obsolete:
view.SetBinding<Model>(View.Text, x=>x.DataModel.Text);

New way: ???? How to do that without magic strings?

Thanks in advance.

Answers

  • WinterCloudWinterCloud GBMember ✭✭✭

    Bump?

  • VelocityVelocity NZMember ✭✭✭

    Use nameof syntax, this will make your code compile-safe and avoid magic strings.

  • WinterCloudWinterCloud GBMember ✭✭✭

    @Velocity said:
    Use nameof syntax, this will make your code compile-safe and avoid magic strings.

    How to do that with a complex view model? the 2nd part of the thread starter post.

  • VelocityVelocity NZMember ✭✭✭

    Eg. Control.SetBinding(MyControl.SomeBindableProperty, new Binding(nameof(Property), source: sourceObject));

  • WinterCloudWinterCloud GBMember ✭✭✭

    @Velocity said:
    Eg. Control.SetBinding(MyControl.SomeBindableProperty, new Binding(nameof(Property), source: sourceObject));

    "source:" how to get this source object from view binding? ViewModel.DataModel?

    Or it has to be something like ((ViewModel)BindingContext).DataModel ?

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    uhhh. use XAML?

  • WinterCloudWinterCloud GBMember ✭✭✭

    @JulienRosen said:
    uhhh. use XAML?

    Too many problems for Xamarin Xaml, not considering it at the moment.

  • JohnHJohnH GBMember ✭✭✭✭✭

    @WinterCloud You can use SetBinding to access properties of properties of your model like this:

    exampleButton.SetBinding(Button.TextProperty, "Person.Name");

    where your BindingContext is a model that has a Person property, which has a Name property etc.

  • WinterCloudWinterCloud GBMember ✭✭✭

    @JohnHair said:
    @WinterCloud You can use SetBinding to access properties of properties of your model like this:

    exampleButton.SetBinding(Button.TextProperty, "Person.Name");

    where your BindingContext is a model that has a Person property, which has a Name property etc.

    Thanks for the reply.

    I know Magic String works, which is as same as using Xaml. Question is how to do that without magic strings?

  • JohnHJohnH GBMember ✭✭✭✭✭

    Something like $"{nameof(Person)}.{nameof(Person.Name)}"?

  • WinterCloudWinterCloud GBMember ✭✭✭

    @JohnHair said:
    Something like $"{nameof(Person)}.{nameof(Person.Name)}"?

    This is what I'm doing now, which set binding is becoming really complex and messy. I'm just wondering if there is any better way to do this.

    This Xamarin's change is really annoying, without any thoughts.

  • EstebanYofreEstebanYofre USMember
    edited September 12

    If the object is very complex.

    use IValueConverter

    txtname.SetBinding (Label.TextProperty, "AdvertisementData", converter: new ConverterName ());
    
    public class ConverterName : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return string.IsNullOrWhiteSpace(((IAdvertisementData)value).LocalName)?"": ((IAdvertisementData)value).LocalName.ToString();
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return "";
            }
        }
    
Sign In or Register to comment.