Shell Navigation using query parameters
I am trying to pass a query parameter in a shell application. The code is below. I have this code decorating the viewmodel
[QueryProperty(nameof(PayerMode), nameof(PayerMode))].
I shouldnt have to bind to anything to pass a parameter to another page, correct?
async Task SavePayer() =>
await Shell.Current.GoToAsync($"{nameof(PayerEditView)}?PayerMode = Edit");
.NET MAUI
C#
-
Lloyd Sheen 1,476 Reputation points
2023-05-09T22:04:03.1966667+00:00 I always use a Dictionary as follows. I know it works and if you want to pass more than one parameter required and it is good as if you find the requirement to pass another parameter later on it is ready.
private async void letsseeifthisworks(object sender, EventArgs e) { MenuFlyoutItem mfi=(MenuFlyoutItem )sender; var navigationParameter = new Dictionary<string, object> { {"theTeamId",0} }; await Shell.Current.GoToAsync($"{nameof(TeamsPage)}", navigationParameter); }
-
Ronald Rex 1,666 Reputation points
2023-05-10T00:17:56.3866667+00:00 Thanks for your help. I was wondering how do I access the parameter that I sent on in your case the TeamsPage? I need access to this parameter before loading the ViewModel to see if I need to call some Methods in the ViewModel Constructor. Thanks !!!
-
Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points • Microsoft Vendor
2023-05-10T06:36:31.9033333+00:00 Can you share your specific usage scenario?
Which platform do you want to access to this parameter before loading the ViewModel?
I test it with
Shell.Current.GoToAsync()
, I cannot access to this parameter before loading the ViewModel. -
Ronald Rex 1,666 Reputation points
2023-05-10T13:53:28.0933333+00:00 My Question would fall under passing Data in Shell Based Application using the .Net Community Toolkit. For example I would like to pass a value that I will access on another viewmodel that will determine the execution of some code. For example I have this Task in my MainViewModel. I want to access the value I set for PayerMode in another Viewmodel that PayerEditView is bound to. Again, I want to access the value for PayerMode on the EditPayerViewModels Constructor like so... I greatly appreciate anyones help.
public PayerEditViewModel(IPyrDirDataAccess pyrDirDataAccess, IWrkDirDataAccess wrkDirDataAccess, IWrkDirQueries wrkDirQueries, IPopupService popupService) { _popupService = popupService; _wrkQueries = wrkDirQueries; _wrkDB = wrkDirDataAccess; _pyrData = pyrDirDataAccess; if (PayerMode=="Edit") { RefreshUserDefienedFieldsList(); } //RefreshStateList(); }
[RelayCommand] #if DEBUG // debug stuff goes here // If a PayerList Entry is Selected then Mode is 1 for edit. Else Mode is 0 for Creating a New Payer async Task SavePayer() => await Shell.Current.GoToAsync($"{nameof(PayerEditView)}?PayerMode = Edit");
-
Lloyd Sheen 1,476 Reputation points
2023-05-10T13:57:24.3666667+00:00 Toget the data passed : (and this is code that is used from my GoToAsync)
1st. You need to add
public partial class TeamsPage : ContentPage, IQueryAttributable
and then
public void ApplyQueryAttributes(IDictionary<string, object> query) { int teamId = 0; NHLTeamView tv = (NHLTeamView)this.theTeamView; ; try { teamId = (int)query["theTeamId"]; } catch (Exception ex) { } if (teamId != 0) { tv.theTeamSelectorMethod(teamId); } else { //tv.Button_Clicked(null,null); } }
-
Ronald Rex 1,666 Reputation points
2023-05-10T14:15:44.3166667+00:00 Where is your GoToAsync? I think I will be okay if you can just tell me the syntax for assigning a value to my ObservalProperty PayerMode while executing this command..... await Shell.Current.GoToAsync($"{nameof(PayerEditView)}?PayerMode = Edit"); The tutorial that I was trying to learn how to do this from was passing values from the Entry Control or a control that the Observable property has a Binding on, but I just want to set my value using a string ?PayerMode = Edit, doesnt come from any Control I have a Binding Property too. I hope you are understanding me. Thanks !
-
Lloyd Sheen 1,476 Reputation points
2023-05-10T14:55:06.4566667+00:00 See my comment above it has the setup of the dictionary and the call to GoToAsync
-
Ronald Rex 1,666 Reputation points
2023-05-10T15:04:25.59+00:00 @Lloyd Sheen As I asked you earlier where are you accessing navigationParameter after you pass it to your TeamsPage? Can I access it in the ViewModel that belongs to the TeamsPage?
-
Lloyd Sheen 1,476 Reputation points
2023-05-10T18:14:56.6866667+00:00 See the comment about the ApplyQueryAttributes. When you navigate to the page this is called. The example I show takes a parameter with the key theTeamId and gets the value. What you do with this value after depends on what you need it for. In your case you are trying to pass a PayerMode so you would create the dictionary add to it the value you want to pass as the value of PayerMode and in the ApplyQueryAttributes access and use it as you need. You can then set it in your ViewModel for the PayerEditView.
-
Ronald Rex 1,666 Reputation points
2023-05-11T21:09:20.6666667+00:00 Thanks Very Much !!!
Sign in to comment