Xamarin.Forms Shell GoToAsync pass a object

RubenGlamRubenGlam Member ✭✭

Hi, i'm using the shell in my app and i tried to navigate a external page using routes but i couldn't pass un object from my model. It throwed a exception saying that the object isn't a string. How i can do it?

Thanks you!

Best Answer

Answers

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @RubenGlam said:
    Hi, i'm using the shell in my app and i tried to navigate a external page using routes but i couldn't pass un object from my model. It throwed a exception saying that the object isn't a string. How i can do it?

    Thanks you!

    Make sure it is a string type ,Check Bold Text below

    (App.Current.MainPage as Xamarin.Forms.Shell).GoToAsync($"app://tailwindtraders/productcategory?id={typeId}", true);

  • RubenGlamRubenGlam Member ✭✭

    @Charwaka I want that typeId isn't a string. I need pass other object (for example a label). That is possible?

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @RubenGlam said:
    @Charwaka I want that typeId isn't a string. I need pass other object (for example a label). That is possible?

    use .ToString to convert Label

  • RubenGlamRubenGlam Member ✭✭
    edited April 4

    @Charwaka
    I didn't get it yet. I have a listview which have a list of items (objects from my model, it have a id, name, image...). When I will click a item from listview i want open a new page using routes and pass the object clicked. Then, in the new page i want get the object and show the name and image for example. I want know if it is possible. Sorry my english is so bad :(

    Ty

  • 15mgm1515mgm15 USMember ✭✭✭✭

    @LandLu said:
    @RubenGlam You could use a static model class to store these data, then retrieve it when you want to display them:

    //Model class
    static SettingsViewModel settingsViewModel { set; get; }
    public static SettingsViewModel SharedModel()
    {
        if (settingsViewModel == null)
        {
            settingsViewModel = new SettingsViewModel();
        }
                
        return settingsViewModel;
    }
    
    // Set value
    SettingsViewModel model = SettingsViewModel.SharedModel();
    model.Id = "ID";// ...Other properties here
    // Get value
    SettingsViewModel model = SettingsViewModel.SharedModel();
    

    Do not try to pass a image here as it is a Forms control. You can pass the image's path instead.
    Or you can convert your mode to a json string first then pass it to the next page:

    var jsonStr = JsonConvert.SerializeObject(model);

    @LandLu Is this the best practice?

Sign In or Register to comment.