lonelydev101
lonelydev101

Reputation: 1901

Xamarin FOrms routing with passing parameters in Shell

In my codeBehind of my Page1 I have:

        {
            //do not select item
            ((ListView)sender).SelectedItem = null;

            //go to stockDetailsPage
            await Shell.Current.GoToAsync("postlogin/stockdetails?tickerSymbol=AAPL.NASDAQ");
        }

As you can see I hardcoded the value in order to test it.

Earlier it was without param, and it work, now I need to pass param tickerSymbol. And in my stockDetailsPage in my ViewModel I added

[QueryProperty("TickerSymbol", "tickerSymbol")] above my class

and in my class I declared properties:

        public string TickerSymbol
        {
            set { SetProperty(ref tickerSymbol, Uri.UnescapeDataString(value)); }
            get { return tickerSymbol; }
        }

So in my constructor of that vm, I now want to call method with input argument of that param, but Im getting null all the time.

Can you suggest me where Im wrong?

Upvotes: 3

Views: 3372

Answers (1)

Lia
Lia

Reputation: 523

As Xamarin doc said it should be able to pass param to the class for the page's BindingContext, I tried the following and it works:

in code behind, set BindingContext to your ViewModel:

TestParamViewModel testParamViewModel;
public TestPage()
{
   InitializeComponent();
   testParamViewModel = new TestParamViewModel();
   this.BindingContext = testParamViewModel;
}

Add QueryPropertyAttribute to your ViewModel:

[QueryProperty("Name", "name")]
class TestParamViewModel
{
    string name;
    public string Name
    {
        set
        {
            name = Uri.UnescapeDataString(value);
        }
    }
}

call navigation:

await Shell.Current.GoToAsync("testpage?name=Abyssinian");

Hope it helps.

Upvotes: 7

Related Questions