1
votes

I have a problem which has taken hours upon hours of my time. I am sure I am missing something obvious. I have reproduced the issue on a simplified form which uses just 1 button and 1 label. Label correctly sets to initial value. Upon button click I am trying to change the label text. From David to Terry.

Command button is firing, setter is getting called, onPropertyChange is getting called. Interestingly after initial debug the get'er is not fired again. (Checked all obvious things, property is public and a property, it is named correctly, specified TwoWay)

...

//--- View Model code ---//
public class TestBindingVM : INotifyPropertyChanged
{
    private string profileName;

    public ICommand ChangeTextCommand { get; }

    public TestBindingVM()
    {
        ProfileName = "David";
        ChangeTextCommand = new Command(UpdateTextCommandAction);
    }

    public void UpdateTextCommandAction()
    {
        ProfileName = "Terry";
    }


    public string ProfileName
    {
        get => profileName;
        set
        {
            profileName = value;
            OnPropertyChanged("ProfileName");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var propertyChangedCallback = PropertyChanged;
        propertyChangedCallback?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
// ---------------------
// Complete XAML Layout :
//----------------------
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="TrackManager.Views.TestBindingPage">
  <ContentPage.Content>
    <StackLayout>
        <Label Text="{Binding Path=ProfileName}"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="CenterAndExpand" />
        <Button Margin="0,10,0,0" Text="Change Text"
                    Command="{Binding ChangeTextCommand}"
                    TextColor="White"
                    />
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

///----------------------------------------------//
// The page code, creating the bindingcontext    //
///----------------------------------------------//
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestBindingPage : ContentPage
{
    public ViewModels.TestBindingVM vm = new ViewModels.TestBindingVM();

    public TestBindingPage ()
    {
        this.BindingContext = vm;
        InitializeComponent();
    }
}

...

This is similar to this post, which did not get a complete answer. Perhaps due to missing code:

Two way binding Not working In Xamarin Forms

It is just so strange that the value "David" is shown on loading. Which shows binding is close to working.

Help appreciated.

1
why are you using TwoWay binding with a Label? TwoWay only makes sense if the control allows the user to interact with it, like an Entry or Picker. A Label is not interactive.Jason
It is set to twoWay so that the code behind can change the text as needed. Also tried with and without the twoWay :-(drHodge
Data --> UI is one way. You don't need to specify that, a Label control will default to that binding. Try just Text="{Binding ProfileName}"Jason
Thanks. Just tried again when I saw your comment. Exactly same behavior. This is going to be something so dumb when it is found.drHodge
no idea. I've used binding thousands of times in Forms projects without a problem. You might want to double check your implementation of INPC vs one of the Microsoft docs, but at a glance it appears correct to me.Jason

1 Answers

0
votes

Just for the record, I found the issue. For some reason there was a local, empty interface of INPC. I guess when using the refactor function I clicked implement interface rather than add 'using'. Therefore when the INPC isn't doing what it clearly should, check you are using the actual INPC not some stub :-)