Not able to pass parameters to a shell content page

Gaurav Gupta 1 Reputation point
2021-03-07T18:07:24.883+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-03-08T09:29:59.993+00:00

    Hello,

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

      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://learn.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.