Using ApplyQueryAttributes Method with Constructor

Tom Meier 140 Reputation points
2024-04-29T21:43:57.18+00:00

How can I use the ApplyQueryAttributes Method of IQueryAttributable to retrieve a value for a parameter in my viewmodel? The problem is that the method I want to use the parameter for is in the constructor and ApplyQueryAttributes has not executed before this method in the constructor is called. Is there a way to fire off ApplyQueryAttributes before the constructor, or should I call the method from within ApplyQueryAttributes? What is the best practice? Here is an example of my code:

The gist of the matter is I am trying to pass parameters from one viewmodel to another, but the method that uses the parameter is in the constructor so it fires before ApplyQueryAttibutes where I get the parameter value.

public void ApplyQueryAttributes(IDictionary<string, object> query)
{
    Package myPackage = query["MyPackage"] as Package;
    Id = myPackage.packageId;
    // My Method
    RefreshListData();
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,965 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,311 Reputation points Microsoft Vendor
    2024-04-30T02:59:51.8866667+00:00

    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.

    0 comments No comments