Passing Parameters using QueryParameter in MAUI

Jassim Al Rahma 1,526 Reputation points
2023-01-04T16:30:11.733+00:00

Hi,

I am trying to pass PackageId and PackageName using QuerParameter in MAUI project but nothing is showing.

Hee is my sample:

https://github.com/jrahma/QueryPropertySample/tree/main/QueryPropertySample

Kindly help..

Thanks,
Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,943 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,320 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,146 Reputation points Microsoft Vendor
    2023-01-05T07:04:24.757+00:00

    Hello,

    if you have multiple parameters like PackageID and PackageName, does this. mean you have to create another method for every parameter?

    You can create an object with PackageID and PackageName attributes. then send this object by QueryProperty.

    For example, I create a Package class.

       public class Package  
       {  
           public string packageId { get; set; }  
          public string packageName { get; set; }  
       }  
    

    Then I send this object by Dictionary in the GoTapGestureRecognizer_Tapped method.

       async void  GoTapGestureRecognizer_Tapped(object sender, EventArgs e)  
           {  
                  Label lbl = (Label)sender;  
              
                  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. Please notice I change the property in QueryProperty and MyPage2.cs like the following 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");  
           }  
       }  
    

    You can refer to this document about Pass data

    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.


0 additional answers

Sort by: Most helpful