question

GauravGupta-1893 avatar image
0 Votes"
GauravGupta-1893 asked JessieZhang-2116 edited

Not able to pass parameters to a shell content page

I have created a shell page and trying to call a content page as per following code in xamarin forms public

  AppShell() { Routing.RegisterRoute("itemdetails", typeof(ItemDetailPage)); }

Following is the event on which i am calling content page - itemdetails page alongwith selected categoryid as parameter

    private async void CategoriesCollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e) 
   {
     int selectedCategoryId = ((CategoryMasterWithImage)e.CurrentSelection[0]).CategoryId; 
     Application.Current.Properties["SelectedCategoryId"] = selectedCategoryId.ToString(); 
     await Shell.Current.GoToAsync($"itemdetails?name={selectedCategoryId}"); 
   
   public ItemDetailPage(int categoryId) 
   { 
       InitializeComponent(); 
       productCategoryId = categoryId; 

    } 

This is constructor of ItemDetailPage.. When i am calling this is saying: No Default Constructor found for ItemDetailPage




dotnet-xamarinforms
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.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered JessieZhang-2116 edited

Hello,


Welcome to our Microsoft Q&A platform!
When you used the following code to pass parameter to next page ItemDetailPage` ,

   await Shell.Current.GoToAsync($"itemdetails?name={selectedCategoryId}"); 

We could get the passed data by using QueryProperty to get the data in next page ItemDetailPage, and we should add a default constructor for Page ItemDetailPage ,for example:

 [QueryProperty(nameof(ProductCategoryId), "name")]
 public partial class ItemDetailPage : ContentPage
 {
     public string ProductCategoryId
     {
         set
         {
             LoadData(value);
         }
     }
    
     void LoadData(string id)
     {
         try
         {
             Console.WriteLine(" the passed data ProductCategoryId = " + id);    
             // set BindingContext for you page    
         }
         catch (Exception)
         {
             Console.WriteLine("Failed to load animal.");
         }
     }
    
     public ItemDetailPage()
     {
         InitializeComponent();
    
         //BindingContext = new ItemDetailViewModel();
     }
 }

For more details, you can check: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#pass-data

You can also refer to the sample :https://github.com/xamarin/xamarin-forms-samples/blob/master/UserInterface/Xaminals/Xaminals/Views/CatDetailPage.xaml.cs


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.