Xamarin FORms 路由,在 Shell 中传递参数 [英] Xamarin FOrms routing with passing parameters in Shell

查看:91
本文介绍了Xamarin FORms 路由,在 Shell 中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Page1 后面的代码中,我有:

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.

之前它没有参数,它可以工作,现在我需要传递参数 tickerSymbol.在我的 ViewModel 的 stockDetailsPage 中,我添加了

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")] 在我的班级之上

在我的班级中,我声明了属性:

and in my class I declared properties:

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

所以在我的那个 vm 的构造函数中,我现在想用那个参数的输入参数调用方法,但我总是得到 null.

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?

推荐答案

As Xamarin doc 说它应该能够将 param 传递给页面的 BindingContext 类,我尝试了以下方法并且它有效:

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:

在后面的代码中,将 BindingContext 设置为您的 ViewModel:

in code behind, set BindingContext to your ViewModel:

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

将 QueryPropertyAttribute 添加到您的 ViewModel:

Add QueryPropertyAttribute to your ViewModel:

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

呼叫导航:

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

希望有帮助.

这篇关于Xamarin FORms 路由,在 Shell 中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆