Shell Navigation and passing Parameter Values

Ronald Rex 1,671 Reputation points
2023-01-26T20:32:49.7633333+00:00

Hi friends, I was wondering how to pass multiple parameters. I can pass one parameter for now... ?Name={Name}

Thanks

Task Navigate() =>
        Shell.Current.GoToAsync($"{nameof(DetailPage)}?Name={Name}",
Developer technologies | .NET | .NET MAUI
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-01-27T00:52:02.6133333+00:00

    Hello,

    If you want to pass multiple parameters, you can send them by Dictionary. Or create an object that contain these parameters. put this Object to the Dictionary and send it, here is a similar thread: Passing Parameters using QueryParameter in MAUI.

     Package package=  new Package() { packageId = lbl.Text, packageName = "yourpageName" };  
     var navigationParameter = new Dictionary<string, object> { { "MyPackage", package } };  
     await Shell.Current.GoToAsync($"MyPage2", navigationParameter);  
    

    You can get this object in the MyPage2's background code.

    [QueryProperty(nameof(MyPackage), "MyPackage")]  
       public partial class MyPage2 : ContentPage  
       {  
           public Package MyPackage  
           {  
               set  
               {  
                   ShowMessage(value);  
               }  
           }  
           public MyPage2()  
           {  
               InitializeComponent();  
               
           }  
         
          public async void ShowMessage(Package package)  
           {  
               var id = package.packageId;  
               var name = package.packageName;  
              await DisplayAlert("Query", id+ name, "Ok");  
           }  
       }
    

    If you want to get these parameters in the ViewModel, you need to achieve IQueryAttributable interface and get parameters in the ApplyQueryAttributes method.

    public class Page2ViewModel : IQueryAttributable
    {
    
        public void ApplyQueryAttributes(IDictionary<string, object> query)
        {
           Package myPackage= query["MyPackage"] as Package;
           var id=  myPackage.packageId;
           var packageName= myPackage.packageName 
        }
     
    }
    

    Here is a document about passing and processing multiple items of data in shell with QueryProperty.

    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.

    5 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.