I have an app in .NET MAUI with XAML for the UI. I converted all the XAML to C# markup except for the AppShell. A sample of what I want to convert is below:
<ShellItem Route="LoginPage">
<ShellContent ContentTemplate="{DataTemplate authentication:LoginPage}"/>
</ShellItem>
<TabBar >
<Tab
Title="{Binding TitleFeed}"
>
<ShellContent
Title="{Binding TitleFeed}"
ContentTemplate="{DataTemplate feed:FeedPage}"
Route="FeedPage" />
</Tab>
<Tab
Title="{Binding TitleSettings}"
>
<ShellContent
Title="{Binding TitleSettings}"
ContentTemplate="{DataTemplate settings:SettingsPage}"
Route="SettingsPage" />
</Tab>
</TabBar>
I am expecting to define the ShellItem (default page) and the TabBar in C# markup and remove the use of XAML.
In particular, I am struggling to define this in C# markup for the ContentTemplate binding to one of the Content Pages. e.g. The ContentTemplate definition below does not work:
ShellContent shellContentFeedPage = new()
{
Title = AppShellViewModel.TitleFeed,
ContentTemplate = (DataTemplate)FeedPage,
Route = "FeedPage"
};
I can't find any examples of how to do this online... Can anyone advise please?
I created a new project to achieve your request. Use the
Items.Add(ShellItem)
in the AppShell can do that.In the code behind:
And the result image:
Update:
Regiter the AppShell DI and the LoginPage:
And then in the App.cs:
And in the AppShell.cs: