How to pass parameter to FlyoutItem->ShellContent?

Pelin Konaray 291 Reputation points
2022-07-20T08:22:20.22+00:00

Hi,

I have an application where I open the same page template as nested. I want to use shell flyout menu in my application. I added this page that I want to open nested in the shell as FlyoutItem->ShellContent. What I want to do is something like the following gif:
222568-20220720-110751.gif

In order to take different actions on this page, I need to send some parameters (for example, I will send an api request within the page and the request I will send changes every time I open the page. I need to send some parameters to be used in this request while pushing the page.)

When I press the button to open the page as nested, I send the parameters using a command like the one below.

await Shell.Current.GoToAsync($"Second{nameof(FileSystemPage)}", new Dictionary<string, object>  
                        {  
                            ["NavigationInfo"] = new NavigationInfo() { DirectoryPath = "FolderName" }  
                        });  

However, when I define this page as FlyoutItem->ShellContent in the shell, I also want to send this parameter. How do I do this?

<Shell  
    x:Class="FileOrbis.Views.AppShell"  
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"  
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
    xmlns:local="clr-namespace:FileOrbis.Views"  
    x:Name="AppShellTest"  
    Shell.TabBarIsVisible="False">  
  
    <ShellContent  
        Title="Home"  
        ContentTemplate="{DataTemplate local:LoginPage}"  
        Route="LoginPage"  
        Shell.FlyoutBehavior="Disabled"  
        FlyoutItemIsVisible="False" />  
  
    <FlyoutItem Route="FileSystemPage" FlyoutDisplayOptions="AsMultipleItems">  
        <ShellContent Title="My Folders" ContentTemplate="{DataTemplate local:FileSystemPage}"/>  <!--I want to send parameter in there-->  
        <ShellContent Title="Public Network Folders" ContentTemplate="{DataTemplate local:FileSystemPage}" /> <!--I want to send parameter in there-->  
        <ShellContent Title="My Settings" ContentTemplate="{DataTemplate local:MySettingsPage}" />  
    </FlyoutItem>  
  
    <Shell.FlyoutFooter>  
        <StackLayout Padding="20">  
            <Button Text="Sign Out" Command="{Binding SignOutCommand}" BackgroundColor="#1D8348" />  
        </StackLayout>  
    </Shell.FlyoutFooter>  
      
</Shell>  

 
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,923 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Gordon Moore 6 Reputation points
    2022-07-23T09:03:30.997+00:00

    A method I have found to be particularly effective at passing objects and even large lists (though I suspect it is by reference rather than by value) around pages is to use the MessagingCenter system. I use it to also indicate that something has happened in one part and needs to be handled in another part e.g. between code behind and ViewModel (not everything is bound!)

    In sender code (String.Empty can be any object)

    MessagingCenter.Send(new EmptyClass(), "SomethingHappened", String.Empty);  
    

    In receiving code (in constructor works well)

    MessagingCenter.Subscribe<EmptyClass, string>(this, "SomethingHappened", (sender, arg) =>  
    {  
        //arg contains the object (in this case a string);  
    });  
    

    this can be any object apparently!
    EmptyClass is just that

    namespace xxx  
      
    public class EmptyClass  
    {  
    }  
    

    There are some questions I asked in Stackoverflow such as https://stackoverflow.com/questions/73005923/messagingcentre-does-it-pass-object-by-value-or-by-reference

    https://stackoverflow.com/questions/72913742/net-maui-how-to-read-write-get-set-a-global-object-from-any-content-page-mv

    Hope this provides a solution to your issue :)