Hello,
should I call the method from within ApplyQueryAttributes?
Yes, you can implement INotifyPropertyChanged
interface in your ViewModel, INotifyPropertyChanged
provides the ability for a class to raise the PropertyChanged
event whenever one of its properties changes. The data binding mechanism in .NET MAUI attaches a handler to this PropertyChanged event so it can be notified when a property changes and keep the target updated with the new value.
For example, you bind the Name in the Label like <Label Text="{Binding Name}"/>
. When MyViewModel
's constructor is exeuted, Label is empty. After executing ApplyQueryAttributes
, Name has a value. Label' Text will be changed at runtime.
public class MyViewModel: IQueryAttributable, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public MyViewModel()
{
}
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged(); }
}
public void ApplyQueryAttributes(IDictionary<string, object> query)
{
Name = query["name"] as string;
}
}
For more details about Data binding and MVVM, please check this document:Data binding and MVVM - .NET MAUI | Microsoft Learn
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.