0
votes

I have a XAML view bound to a ViewModel

When the view loads it sets the binding context to be an object (type GAME_TBL)

When the ViewModel loads, I await a message arriving with a unique ID

I then use this to calll an API and return a game object which I cast to replace the one that is bound

However, even tho debug shows me this is all working, and the PropertyChanged fires, the UI never updates

From the View:

<ContentPage.BindingContext>

    <viewmodels:VotingViewModel/>

</ContentPage.BindingContext>

.....

<Label Text="{Binding currentGame.GAME_NAME}" />

In the VotingViewModel:

public class VotingViewModel : BaseViewModel

{

    GAME_TBL gameSelected;

    public GAME_TBL currentGame
    {
        get {

            return _currentGame;


        }


        set
        {
            _currentGame = value;
            OnPropertyChanged(nameof(_currentGame));
        }
    }

    public VotingViewModel()
    {
        MessagingCenter.Subscribe<Views.GamesView, string>(this, "Selected Game", (sender, arg) =>

        {
            gameID = Convert.ToInt32(arg);

            currentGame = loadGameInfo(); // this interacts with an API to set object specifics

        });

    }

}

In the BaseViewModel

    protected virtual void OnPropertyChanged(
    string propertyName = null)
    {
        System.Diagnostics.Debug.WriteLine("Debug: Name of property: " + propertyName);

        PropertyChanged?.Invoke(this,
        new PropertyChangedEventArgs(propertyName));


    }
1
nameof(_currentGame) is the internal backing field, NOT the public property you are binding to - Jason
Oh FFS, that's fixed it. Thanks so much - Journeyman1234
@Journeyman1234 You can mark the right answer which will help more people with same problem. - Jack Hua
I cannot see how, only comments show as Answers, not replies to my post? - Journeyman1234

1 Answers

0
votes

You need to inherit BaseViewModel in the Model Class of GAME_TBL. And inside the class GAME_TBL, you need to specify each and every property in the following manner:

    private string _gAME_NAME;
    public string GAME_NAME
    {
        get => _gAME_NAME;
        set
        {
            _gAME_NAME = value;
            OnPropertyChanged(nameof(GAME_NAME));
        }
    }

Each property should have a an internal backing property, as above.