Xamarin.forms-使用Shell导航时传递对象

wgx48brx  于 2022-12-07  发布在  Shell
关注(0)|答案(4)|浏览(173)

我正在使用shell,我希望所有页面上都有标签。所以我遵循shell导航的标准方式,但问题是我不知道如何在导航中传递对象。
await Shell.Current.GoToAsync(page.ToString());
为导航执行此操作
Routing.RegisterRoute("TesProject.Views.DetailView", typeof(DetailView));
这样注册路由
我想将一个完整的对象从列表视图传递到详细视图。我该怎么做?
Xamarin.Forms Shell Navigation Hierarchy with parameters
我看到了这个,但我认为这在我的情况下不起作用,因为我不能像这样传递一个完整的模型对象。

zz2j4svz

zz2j4svz1#

我写了一个小例子供大家参考。
在发送类中,可以传递$"AferPage?param={param}"之类的参数。
下面是发送代码:

public partial class BeforePage : ContentPage
{
    public BeforePage()
    {
        InitializeComponent();
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        string param = myEntry.Text;
        await Shell.Current.GoToAsync($"AferPage?param={param}");//like this
    }
}

下面是接收类代码(实现IQueryAttributable接口):

public partial class AferPage : ContentPage, IQueryAttributable
{
    public string param  {get;set;}
    public void ApplyQueryAttributes(IDictionary<string, string> query)
    {
        param = HttpUtility.UrlDecode(query["param"]);
        receive.Text = param;
    }
    public AferPage()
    {
        InitializeComponent();  
    }
}
6ovsh4lw

6ovsh4lw2#

使用Newtonsoft.Json,您可以:

在列表视图中:

var jsonStr = JsonConvert.SerializeObject([Model]);
await Shell.Current.GoToAsync($"{nameof([DetailsViewPage])}?Param={jsonStr }");

在详细信息视图页中:

添加查询属性:

[QueryProperty(nameof(Param), nameof(Param))]

再次转换为模型:

var bonusesFilterData = JsonConvert.DeserializeObject<[Model]>(Param);

此视频中显示了一种解决方案:https://www.youtube.com/watch?v=YwnEpiJd0I8

pwuypxnk

pwuypxnk3#

可以将对象作为字典传递。
例如,如果这是您要发送的数据对象:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

在页面的视图模型上设置查询属性:

[QueryProperty(nameof(Person), nameof(Person))]
public partial class DetailViewModel
{
    [ObservableProperty]
    Person person;
}

假设您的页面构造函数如下所示,它将自动设置上下文的值:

public partial class DetailView : ContentPage
{
    public DetailView(DetailViewModel vm)
    {
        InitializeComponent();
        BindingContext = vm;
}

然后,当您巡览至页面时,将对象传入字典中:

Routing.RegisterRoute(nameof(DetailView), typeof(DetailView));
await Shell.Current.GoToAsync(nameof(DetailView), {
    new Dictionary<string, object> {
        [nameof(Person)] = person
    });

现在您可以访问绑定中的对象:

<Label Text="{Binding Person.Name}"/>
<Label Text="{Binding Person.Age}"/>

注意使用nameof来避免硬编码字符串。

lrl1mhuk

lrl1mhuk4#

如果json比较复杂,可以使用存储的首选项,例如:

private async void OnItemSelected(Item item)
    {
        if (item == null)
            return;

        var jsonstr = JsonConvert.SerializeObject(item);

        //Clear the shared preferences in case there is any
        Preferences.Clear();

        //Store your complex json on a shared preference
        Preferences.Set("Data", jsonstr);
        await Shell.Current.GoToAsync(nameof(DetailsPage));
       
       
    }

在检索数据的详细信息页上,可以使用以下代码:

bool hasKey = Preferences.ContainsKey("Data");
            var content = Preferences.Get("Data", string.Empty);

            Details details = hasKey ? JsonConvert.DeserializeObject<Model>(content) : null;

相关问题