question

DanielKraut avatar image
0 Votes"
DanielKraut asked JessieZhang-2116 commented

ViewModel with QueryProperty initialization order

If I navigate to a page with Shell.Current.GoToAsync("page?param=value") then usually the order of my ViewModel initialization is like this:

  • the constructor is called

  • QueryProperty param is set

  • OnAppearing is called

Is this order guaranteed by design? Can I be sure that OnAppearing cannot start before all query properties are assigned to?




dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi anonymous user ,I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·

1 Answer

JessieZhang-2116 avatar image
1 Vote"
JessieZhang-2116 answered

Hello,


Welcome to our Microsoft Q&A platform!

For this, you can check the offical document here Xamarin.Forms Shell navigation .
Especially part Process navigation data using query property attributes.

Take offical sample CatDetailPage.xaml.cs for example,we added some comment to print the log:

 [QueryProperty(nameof(Name), "name")]
 public partial class CatDetailPage : ContentPage
 {
     public string Name
     {
         set
         {
             LoadAnimal(value);
         }
     }
    
     public CatDetailPage()
     {
         InitializeComponent();
    
         System.Diagnostics.Debug.WriteLine("***********1 CatDetailPage  ******");
     }
    
     void LoadAnimal(string name)
     {
         System.Diagnostics.Debug.WriteLine("***********2 pass data  ******");
    
         try
         {
             Animal animal = CatData.Cats.FirstOrDefault(a => a.Name == name);
             BindingContext = animal;
         }
         catch (Exception)
         {
             Console.WriteLine("Failed to load animal.");
         }
     }
    
    
     protected override void OnAppearing()
     {
         base.OnAppearing();
    
         System.Diagnostics.Debug.WriteLine("***********3 OnAppearing  ******");
     }
 }

When we use following code in page CatsPage to navigate to this page(`CatDetailPage`).

   await Shell.Current.GoToAsync($"catdetails?name={catName}");

Page CatDetailPage will be created first ,so the constructor will be triggered first.

After that, after the creation(`constructor` ) is done, the parameters are passed in.

From document Getting Started with XAML, we will know what is that InitializeComponent method in the constructor of Page ( here is CatDetailPage) is :

The constructor of that class calls InitializeComponent, which then calls the LoadFromXaml method that extracts the XAML file (or its compiled binary) from the .NET Standard library. LoadFromXaml initializes all the objects defined in the XAML file, connects them all together in parent-child relationships, attaches event handlers defined in code to events set in the XAML file, and sets the resultant tree of objects as the content of the page.

In other words, the function OnAppearing is triggered only when the page layout is fully loaded.


Best Regards,


Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.



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.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.