Search code examples
xamlxamarinxamarin.formsxamarin-shell

Xamarin XAML: calling a Shell page with a parameter


Suppose you have the following AppShell.xaml:

<TabBar>
<ShellContent Title="Page1" ContentTemplate="{DataTemplate local:Page1}" />
<ShellContent Title="Page2" ContentTemplate="{DataTemplate local:Page2}">
</TabBar>

Also suppose Page2 is identical to Page1 except for one property, which can be easily parameterized. So I'd like to be able to do this:

<TabBar>
<ShellContent Title="Page1" ContentTemplate="{DataTemplate local:Page1}" />
<ShellContent Title="Page2" ContentTemplate="{DataTemplate local:Page1}" 
ContentTemplateParameter="somevalue">
</TabBar>

Problem is, there is no such thing as ContentTemplateParameter. So is there a way to achieve this functionality? The critical part here is that I need to call it from an App-level Tab bar, not from one page to another.

P.S. See my solution below.


Solution

  • Answering my own question; I have come up with a solution.

    1. Assign one of the links a route name:
    <TabBar>
    <ShellContent Title="Page1" ContentTemplate="{DataTemplate local:Page1}" />
    <ShellContent Title="Page2" ContentTemplate="{DataTemplate local:Page1}" 
    Route ="somevalue">
    </TabBar>
    
    1. Then in Page1's constructor you can check whether that name was in the route:
    if(Shell.Current.CurrentItem.CurrentItem.Route.Contains("somevalue")) {
       // do something
    } else { 
       // do something else
    }