Rauf
Rauf

Reputation: 12842

Xamarin Shell : Pass multiple arguments to another page

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();
        }

Upvotes: 2

Views: 4758

Answers (3)

Denys Kazakov
Denys Kazakov

Reputation: 505

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");
}

Upvotes: 4

Cfun
Cfun

Reputation: 9691

As explained in https://learn.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; }
        }

Upvotes: 4

user666
user666

Reputation: 318

you can use parameters in the detailPage

public DetailPage(string val1,string val2)
{

}

then call it in MyPage

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

Upvotes: 0

Related Questions