Shell Navigation and passing Parameter Values

Ronald Rex 1,666 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}",
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,216 questions
C#
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.
10,633 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,936 Reputation points Microsoft Vendor
    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