Xamarin Shell : Pass multiple arguments to another page

Viewed 3174

I have two pages in my Xamarin Forms Shell app. One list page and another is the details page. when I select the item in list page, the detail page will be shown. I was able to pass one parameter to the second page. I know how to pass the second value. But how should I receive the second value in the first property itself.

List Page:

        async private void myLines_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var line = (Models.QLines)e.Item;
            int pno = line.PageNo;
            int lno = line.LineNo;
            await Shell.Current.GoToAsync($"//mainTabs/pages?pageno={pno}&lineno={lno}");
        }

Detail Page:

        public int CurrentPage { get; set; }
        public int CurrentLine { get; set; }
        public bool IsFromSearchPage { get; set; }

        public string PageNo
        {
            set
            {
                CurrentPage = Convert.ToInt32(Uri.UnescapeDataString(value));
                IsFromSearchPage = true;

                LoadPagesAsSingle();
            }
        }

        public string LineNo
        {
            set
            {
                CurrentLine = Convert.ToInt32(Uri.UnescapeDataString(value));
            }
        }

        public MyPages()
        {
            InitializeComponent();

            conn = DependencyService.Get<ISQLiteMyConnection>().GetConnection();

            IsFromSearchPage = false;

            LoadPagesAsSingle();
        }
3 Answers

As explained in https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#pass-data you can use QueryProperty attribute to map between queryID and the target property:

[QueryProperty("Pageno", "pageno")]
[QueryProperty("Lineno", "lineno")]

public partial class DetailPage: ContentPage
{

        private int _Pageno;
        public int Pageno
        {
            get { return _Pageno; }
            set { _Pageno = value; }
        }

        private int _Lineno;
        public int Lineno
        {
            get { return _Lineno; }
            set { _Lineno = value; }
        }

As another solution you can pass parameters through static members of page. For example page may contain static ViewModel object, which will be initialized before page appears.

    public class ViewModel : ViewModelBase   
    {
        public string Text { get; set; }
        public int Number { get; set; }
    }

    public class MyPage : Page
    {
        static ViewModel _viewModel = new ViewModel();

        public MyPage() 
        {
            BindingContext = _viewModel;
        }

        public static void InitPage(string text, int number)
        {
            _viewModel.Text = text;
            _viewModel.Number = number;
        }
    }
}

Code inside caller

static async Task GotoPage()
{
    MyPage.InitPage("Text", 123);
    await Shell.Current.GoToAsync($"//mainTabs/pages");
}

you can use parameters in the detailPage

public DetailPage(string val1,string val2)
{

}

then call it in MyPage

Navigation.PushAsync(new DetailPage("parameter1","parameter2"));
Related