Forum Xamarin Xamarin.Forms

Shell navigation with parameter

XamerDevXamerDev Member ✭✭
edited April 15 in Xamarin.Forms

Hello, I have a question
I have simple app in MVVM/Shell which has two pages. First page has listview with some objects, and the second page is detail page.
When user tapped on some item on listview, second detail page with some charts etc. should appear with data of this selected item. Simple
On Page1.cs I have OnItemSelected method with shell navigation:
await Shell.Current.GoToAsync($"pagedetails?name={objectName}"); - simple it's works
I want to pass this name to Page2ViewModel so I use:
[QueryProperty("ObjectName", "name")]
But in Page2ViewModel I have a command with sqlite query. I need to get data, compute quantity and so on and pass values to charts.
how should I do that? I can't get the value because ViewModel initialization starts before value from query property is set.
Please help me :)

Tagged:

Best Answer

  • XamerDevXamerDev Member ✭✭
    edited April 17 Accepted Answer

    I think that I resolved my problem. I added query property to Page2.xaml.cs and pass ID to it. There I have Property which get this ID. After that i override OnApearing method and set BindingContext to ViewModel here. And pass ID through it to viewModel :)

    What do you think about this solution?
    Sample code (from my head :P)

    [QueryProperty("ObjectID", "id")]
    public partial class Page2Page : ContentPage {...
    public string ObjectID
    {
    get
    {
    return obID;
    }
    set
    {
    odID=
    Uri.UnescapeDataString(value);
    }
    }

    protected override void OnAppearing()
    {
    base.OnAppearing();
    BindingContext = viewModel = new Page2ViewModel(odID);

                viewModel.LoadChartCommand.Execute(null);
    
        }
    

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    I can't get the value because ViewModel initialization starts before value from query property is set.

    You could try using Await-Async to ensure the sqlite query completed before ViewModel initialization.

    If it doesn't work, could you post the related code about the sqlite query and viewModel initialization? It'll help to reprduce the issue for a solution.

  • XamerDevXamerDev Member ✭✭
    edited April 17 Accepted Answer

    I think that I resolved my problem. I added query property to Page2.xaml.cs and pass ID to it. There I have Property which get this ID. After that i override OnApearing method and set BindingContext to ViewModel here. And pass ID through it to viewModel :)

    What do you think about this solution?
    Sample code (from my head :P)

    [QueryProperty("ObjectID", "id")]
    public partial class Page2Page : ContentPage {...
    public string ObjectID
    {
    get
    {
    return obID;
    }
    set
    {
    odID=
    Uri.UnescapeDataString(value);
    }
    }

    protected override void OnAppearing()
    {
    base.OnAppearing();
    BindingContext = viewModel = new Page2ViewModel(odID);

                viewModel.LoadChartCommand.Execute(null);
    
        }
    
  • JarvanJarvan Member, Xamarin Team Xamurai
    edited April 17

    I think that I resolved my problem. I added query property to Page2.xaml.cs and pass ID to it ... pass ID through it to viewModel

    Congrats! Getting the value and passing it when initialing, the value will be able to retrieved from construtor method. If you've solved the issue, please mark your solution as the answer. It'll help others who face the similar problem.

  • XamerDevXamerDev Member ✭✭

    Ok, thank YOU :)

Sign In or Register to comment.