在页面之间传递数据Xamarin Shell导航

发布于 2025-01-27 04:59:46 字数 366 浏览 3 评论 0原文

我有一个Xamarin Forms应用程序,其表单在多个页面上分开,我想将对象数据传递到下一个或上一页。我正在使用Xamarin Shell导航。我可以使用什么方法或设置来实现这一目标?

我知道的选项以及我对它们的感知问题:

  • json串起对象并将其作为参数传递。

这似乎是不正确的,因为数据正在来回转换。

  • 将对象的每个属性作为单个参数传递。

长时间缠绕着许多特性,并且不灵活地变化。

  • 将数据存储到SQLITE数据库中。

我不想在表中存储不完整的记录并使用当前的sqliteasyncconnection,我不相信我可以从同一类中创建2个表。

I have a Xamarin forms app with a form split across multiple pages, I want to pass the object data to the next or previous page. I am navigating using the Xamarin Shell. What method or setup can I use to achieve this?

The options I am aware of and my perceived issues with them:

  • JSON string the object and pass it as a parameter.

This seems incorrect as the data is being converted back and forth.

  • Pass every property of the object as an individual parameter.

Massively long winded with many properties and inflexible to change.

  • Store the data to a SQLite database.

I would not want to store an incomplete record in the table and using the current SQLiteAsyncConnection, I don't believe I can have 2 tables created from the same class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念 2025-02-03 04:59:47

是的,您可以使用查询属性属性传递数据。

可以通过为每个查询参数的QueryPropertyAttribute装饰接收类,可以收到导航数据。

有关更多信息,请检查: https://learn.microsoft.com/en-us/xamarin/xamarin/xamarin-forms/app-fundamentals/shell/navigation#process-process-process-navigation-data-using-query-query-query-property-attributes

此外,另一种方法是在应用程序中创建全局可变,您可以在应用程序中访问此可变性。

例如:

1。Create class myVariables.cs,并为您的模型添加静态变量(例如myViewModel):

public  class MyVariables
{
    public static MyViewModel myViewModel { get; set; } = new MyViewModel { Name = "test1" };
}

myviewmodel.cs

public class MyViewModel
{
    public string Name { get; set; }
}

2.您可以在应用程序中修改或访问您的变量:

 // modify the variable
 MyVariables.myViewModel.Name = "test2022";

// access the variable
  System.Diagnostics.Debug.WriteLine("the data is: " + MyVariables.myViewModel.Name);

Yes,you can pass data using query property attributes .

Navigation data can be received by decorating the receiving class with a QueryPropertyAttribute for each query parameter.

For more, check:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#process-navigation-data-using-query-property-attributes .

In addition,another method is to create a global varible in your app, them you can access this varible in your app.

For example:

1.create class MyVariables.csand add static variable for your model (e.g. MyViewModel) :

public  class MyVariables
{
    public static MyViewModel myViewModel { get; set; } = new MyViewModel { Name = "test1" };
}

MyViewModel.cs

public class MyViewModel
{
    public string Name { get; set; }
}

2.You can modify or access your variable in your app:

 // modify the variable
 MyVariables.myViewModel.Name = "test2022";

// access the variable
  System.Diagnostics.Debug.WriteLine("the data is: " + MyVariables.myViewModel.Name);
尾戒 2025-02-03 04:59:47

不幸的是,我们现在只能传递简单的数据。此功能将来会添加: https://github.com/xamarin/ xamarin.forms/desiss/6848 您可以创建多个QueryPropertyAttribute来访问其他数据: https://learn.microsoft.com/en-us/xamarin/xamarin/xamarin-forms/app-fundamentals/shell/navigation#pass-pass-pass-pass-pass-dass-data 另一种方法是将对象转换为JSON字符串,例如:

var jSonstr = jsonconvert.serializeObject(model);
然后在导航时通过它。

Unfortunately, we could only pass simple data now. And this feature will be added in the future: https://github.com/xamarin/Xamarin.Forms/issues/6848 You can create multiple QueryPropertyAttribute to access different data: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#pass-data Another approach is to convert the object to a JSON string like:

var jsonStr = JsonConvert.SerializeObject(model);
Then pass it when navigating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文