Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

maui: changing a bound string from code does not update ui unless string is regenerated (mvvm)

This is my string:

public string EmptyViewText { get; set; } = "Please input a bin id first!*";

I have a simple command:

public async Task Entry(string barcode)
{
    MainThread.BeginInvokeOnMainThread(() =>
    {
        EmptyViewText = "Loading data...*";
        OnPropertyChanged();
    });
}

This is triggered.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is my class:

public partial class BinViewModel : ObservableObject

The binding itself works, as I do see my values. But when I change the values from code, nothing happens.

The binding is super simple:

<Label Text="{Binding EmptyViewText}"  />

If I however disable the string and reenable it (re render it) it does show up with the new and correted text.

What is of issue here?

>Solution :

Your code is calling OnPropertyChanged() without an argument. Wherever that method is, it’s probably defined similar to this:

protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
    PropertyChanged?.Invoke( this, new PropertyChangedEventArgs(propertyName));
}

The caller in this case is Entry not EmptyViewText. It’s going to fire a property changed event, but on the wrong property.

See if it makes a difference if you call it with an explicit argument:

public async Task Entry(string barcode)
{
    MainThread.BeginInvokeOnMainThread(() =>
    {
        EmptyViewText = "Loading data...*";
        OnPropertyChanged(nameof(EmptyViewText));
    });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading