Home > Mobile >  Xamarin.Forms Shell: Passing objects from one vm to another
Xamarin.Forms Shell: Passing objects from one vm to another

Time:10-16

I see how to pass data in the form of strings from one object to another

What I want to do, however, is pass an object from one vm to another -- is this possible? I can think of these ideas:

  1. Stream the object as json and pass that as a param.
  2. Use an id to look up and create the object in the receiving vm
  3. Pass the object itself (ideal)

Will any of these work (well, #2 almost certainly will)? If 1 or 3 will work, can you possibly supply (detailed?) instructions on how to do so?

Thanks!! jesse liberty

CodePudding user response:

Add an parameter in your page constructor like:

 public class MyPage : ContentPage
{
public MyPage(Object sth)
{..... }}

Then pass the object with

Navigation.PushAsync(new MyPage(sth));
  

CodePudding user response:

  1. Put the following at the top of the page that will be receiving the data

     [QueryProperty(nameof(CodesToAdd), nameof(CodesToAdd))]
    
  2. Create a corresponding property (in this case CodesToAdd)

  3. In the setter for that property getg the string value passed to you

    _codesToAdd = Uri.UnescapeDataString(value ?? string.Empty);

To send the data (in this case the codes to add) put this in the calling program:

await Shell.Current.GoToAsync($"{nameof(AddWidgetPage)}?CodesToAdd={codesToAddAsString}");

The first part in the parens identifies the page you want to navigate to. The part after the ? is the parameter. Make sure your param name (CodesToAdd) matches the one in your receiving page.

  • Related