locked
BindingSource of control changes when added into a view RRS feed

  • Question

  • User372105 posted

    I am trying to set a binding in C#. It's initialised once properly and then I get a warning that the control is looking into a different ViewModel for the property and does not update. Is my view somehow overwriting the binding context of the control that I set in C#, if so how can I tell it to keep i'ts binding?

    It does not seem to happen for controls that are binded in xaml

    Thursday, May 14, 2020 8:48 AM

Answers

  • User372105 posted

    With thread I mean those times the device can switch over to another thread. This happened for me when listening for the sensor updates for instance - meaning Device.BeginInvokeOnMainThread(() => {/* update ui*/}) is need - or if you are inside a Tasketc

    In the end I moved from a custom propertychanged and NObservable setup to reactiveui. It worked to put a subscribe the the DataTemplate: ``` _distanceTemplate = new DataTemplate(() => {

                        var distance = new Label() { Text = Distance, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.EndAndExpand, BindingContext = this };
                        // normal bidning with reactiveui did not work so using below instead
                        LocationService.Instance.Location.WhenAnyValue((l) => l.Latitude, (l) => l.Longitude).Subscribe((loc) =>
                        {
                            distance.Text = _Distance(new Location(loc.Item1, loc.Item2));
                        });
                        distance.Text = _Distance(LocationService.Instance.Location);
                        return distance;
                    });
    

    ``` meaning the control will update every time the location changes

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, May 18, 2020 8:32 PM

All replies

  • User371688 posted

    BindingSource of control changes when added into a view Sorry, I don't quite understand what you mean. What do you mean by this words when added into a view ? Could you please post more details about this?

    I get a warning that the control is looking into a different ViewModel for the property and does not update.

    Besides, how can we reproduce this question, could you please please post the steps or a basic demo so that we can try to reproduce this question on our side? Thanks.

    Friday, May 15, 2020 3:31 AM
  • User372105 posted

    Children.Add(control) Content = control was my meaning the with adding control into a view.

    There was a mistake on my part as I did not have set the correct bindingcontext. I am still experiencing my original problem though.

    • ViewModel implementing PropertyChanged
    • DataTemplate in C# returning a Label. SetBinding inside the DataTemplate to a property in the ViewModel
    • Create a Label and add to a Layout

    ---> Changing the property in the ViewModel does not update the Label

    ... After checking in a new xamarin forms project i'ts working though. So it must be something else, possibly a threading issue. Its works to bind a control inside a DataTemplate like below

    DataTemplate LabelTemplate => new DataTemplate(() => { var newLabel = new Label(); newLabel.SetBinding(Label.TextProperty, new Binding(nameof(ViewModel.Name))); return newLabel; });

    If I am correct the signaling to the ui is not made if the property is changed from another thread right? It could be the problem.

    Saturday, May 16, 2020 10:08 AM
  • User371688 posted

    If I am correct the signaling to the ui is not made if the property is changed from another thread right? It could be the problem.

    It is a little abstract, can you explain your question more clearly? What's the another thread in your words and how did you change the property from another thread?

    Monday, May 18, 2020 9:33 AM
  • User372105 posted

    With thread I mean those times the device can switch over to another thread. This happened for me when listening for the sensor updates for instance - meaning Device.BeginInvokeOnMainThread(() => {/* update ui*/}) is need - or if you are inside a Tasketc

    In the end I moved from a custom propertychanged and NObservable setup to reactiveui. It worked to put a subscribe the the DataTemplate: ``` _distanceTemplate = new DataTemplate(() => {

                        var distance = new Label() { Text = Distance, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.EndAndExpand, BindingContext = this };
                        // normal bidning with reactiveui did not work so using below instead
                        LocationService.Instance.Location.WhenAnyValue((l) => l.Latitude, (l) => l.Longitude).Subscribe((loc) =>
                        {
                            distance.Text = _Distance(new Location(loc.Item1, loc.Item2));
                        });
                        distance.Text = _Distance(LocationService.Instance.Location);
                        return distance;
                    });
    

    ``` meaning the control will update every time the location changes

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, May 18, 2020 8:32 PM
  • User371688 posted

    Yes,if we want to update ui in another thread, we need to use the following code as you mentioned.

    Device.BeginInvokeOnMainThread(() => {/* update ui*/})
    

    And I'm glad you solved your problem. And could you please mark this thread to finish this topic? Thanks in advance.

    Tuesday, May 19, 2020 7:14 AM