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.
CodePudding user response:
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://docs.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.
CodePudding user response:
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://docs.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.cs
and 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 modify 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);