Search code examples
c#xamlmaui

How to pass a constructor argument to a Page using ContentTemplate


I am trying to pass a constructor argument to a ContentPage inside a Shell like so:

<FlyoutItem Title="Page1" >
    <ShellContent Title="Page1"
                  Route="Page1" ContentTemplate="{DataTemplate views:Page1}"/>
</FlyoutItem>

I am unable to find a way to accomplish this.

The following code works, but it loads the page on application startup which is too slow.

<FlyoutItem Title="Page1" >
    <ShellContent Title="Page1"
                  Route="Page1">
        <views:Page1>
            <x:Arguments>
                <x:Int32>
                    0
                </x:Int32>
            </x:Arguments>
        </views:Page1>
    </ShellContent>
</FlyoutItem>

I need a way to pass a constructor argument using the ContentTemplate/DataTemplate method


Solution

  • The solution is to pass in all data through a DataTemplate like this:

    <FlyoutItem Title="Page1" >
        <ShellContent Title="Page1"
                      Route="Page1">
            <ShellContent.ContentTemplate >
                <DataTemplate>
                    <views:Page1>
                        <x:Arguments>
                            <x:Int32>
                                0
                            </x:Int32>
                        </x:Arguments>
                    </views:Page1>
                </DataTemplate>
            </ShellContent.ContentTemplate>
        </ShellContent>
    </FlyoutItem>