Search code examples
maui

How to add params to FlyOutItem on ShellContent


I have a ContentPage with this class constructor:

public AssignedTasksPage(string tab)
 {
    InitializeComponent();
    _viewModel = new MvTareas();
    BindingContext = _viewModel;

    if (!string.IsNullOrEmpty(tab))
    {
        switch (tab)
        {
            case "assignedTasks":
                _viewModel.ShowAsignadas();
                break;
            case "inProgressTasks":
                _viewModel.ShowEnCurso();
                break;
            case "completedTasks":
                _viewModel.ShowFinalizadas();
                break;
            default:
                _viewModel.ShowAsignadas();
                break;
        }
    }
   

 }

As you can see it shows the CollectionView of Task in the same pagecontent according to the param it receives.

It work perfectly from my main application page, i can open the ContentPage AssignedTasksPage and put the user in the right place.

But when i tried to open this page from the SideBar flyout my app crashes:

 <ShellContent ContentTemplate="{DataTemplate local:AssignedTasksPage}"  Icon="userblanco.png"    Shell.NavBarIsVisible="False"  Title="Tareas Asignadas" Route="assignedTasks" />

 <ShellContent ContentTemplate="{DataTemplate local:AssignedTasksPage}" Icon="encursoblanco.png" Shell.NavBarIsVisible="False" Title="Tareas En Curso" Route="inProgressTasks" />

 <ShellContent ContentTemplate="{DataTemplate local:AssignedTasksPage}" Icon="tareasblanco.png" Shell.NavBarIsVisible="False" Title="Tareas Finalizadas"  Route="completedTasks"/>

How can i send the "tab" param in the code i am showing above to the AssignedTasksPage Constructor?


Solution

    1. when you tried to open this page from the SideBar flyout,the parameterless constructor was executed by default, which caused a crash.

    2. By using the overridden OnAppearing method to get the route for the current page and then execute the corresponding logic.

    3. We prevent repeated execution of onappearing via flag.

    Here is my code.

      public partial class MainPage : ContentPage
      {
          MvTareas _viewModel = null;
          bool flag = false;
          public MainPage() {
              flag = true;
              InitializeComponent();
          }
          public MainPage(string tab)
          {
              flag = true;
              InitializeComponent();
          }
          protected override void OnAppearing()
          {
              base.OnAppearing();
              if (!flag) {
    
                  string state = Shell.Current.CurrentState.Location.ToString();
                  _viewModel = new MvTareas();
                  BindingContext = _viewModel;
                  if (state != null && state.StartsWith("//"))
                  {
                      state = state.Substring(2);
                  }
                  //remove //
                  if (!string.IsNullOrEmpty(state))
                  {
                      switch (state)
                      {
                          case "assignedTasks":
                              _viewModel.ShowAsignadas();
                              break;
                          case "inProgressTasks":
                              _viewModel.ShowEnCurso();
                              break;
                          case "completedTasks":
                              _viewModel.ShowFinalizadas();
                              break;
                          default:
                              _viewModel.ShowAsignadas();
                              break;
                      }
                  }
              }
              flag = true;
          }
      }