Xamarin.Forms Shell Application: Pass parameter from one page to another page

Jerry Lipan 916 Reputation points
2022-05-30T06:10:47.243+00:00

Hi. I'm Xamarin Forms Newbies. So far, I can navigate from one to another page. Here it is,

206519-20220530-134042.gif

  1. NoticeBoardGetPage.xaml
  2. NoticeBoardEditPage.xaml

NoticeBoardGetPage.xaml navigate to NoticeBoardEditPage.xaml using below code,

 private async void OnSave(object obj)  
        {  
            await Shell.Current.GoToAsync(nameof(NoticeBoardEditPage));  
        }  

Next step,

I modify NoticeBoardEditPage.xaml to make it this page must receive parameter from NoticeBoardGetPage.xaml. Something like this,

 public partial class NoticeBoardEditPage : ContentPage  
    {        
        private string _Id;  
        public NoticeBoardEditPage(string Id)  
        {  
            InitializeComponent();  
            _Id = Id;  
  
            BindingContext = new NoticeBoardEditPageViewModel(_Id);  
        }  
    }  

NoticeBoardEditPageViewModel.cs

  public class NoticeBoardEditPageViewModel  
    {  
        public NoticeBoardEditPageViewModel(string Id)  
        { }  
  
  
  
    }  

Let say in NoticeBoardGetPage, I want send parameter named Id. Something like this,

 private async void OnSave(object obj)  
        {  
            string Id = "42";  
  
            //await Shell.Current.GoToAsync(nameof(NoticeBoardEditPage));  
        }  

How to modify,

await Shell.Current.GoToAsync(nameof(NoticeBoardEditPage));  

Please help

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2022-05-31T02:56:39.75+00:00

    Hello,​

    If you want to transfer ID to ViewModel of navigated page. You can use Process navigation data using a single method to achieve it.

    Firstly, please add Id when you navigated page.

       private async void OnSave(object obj)  
                {  
                    string Id = "42";  
                    await Shell.Current.GoToAsync($"{nameof(NoticeBoardEditPage)}?Id={Id}");  
                }  
    

    Then, please remove _Id in your NoticeBoardEditPageViewModel constructor like this code BindingContext = new NoticeBoardEditPageViewModel();

    In the end. Navigation data can be received by implementing the IQueryAttributable interface on the NoticeBoardEditPageViewModel.

       public class NoticeBoardEditPageViewModel : IQueryAttributable  
           {  
               
               public NoticeBoardEditPageViewModel()  
               {  
                    
               }  
         
               public void ApplyQueryAttributes(IDictionary<string, string> query)  
               {  
                    
       //get the Id here.  
                   string id = HttpUtility.UrlDecode(query["Id"]);  
                    int ID= int.Parse(id);  
               }  
           }  
    

    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jerry Lipan 916 Reputation points
    2022-05-31T14:57:57.523+00:00

    Hi @Anonymous

    It is stunning

    0 comments No comments

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.