Using the Xaminals example, I'm trying to set the default startup page. Here's the relevant code from the xaml:
<FlyoutItem Route="animals" x:Name="shellAnimals"
Title="Animals"
FlyoutDisplayOptions="AsMultipleItems">
<Tab Title="Domestic" x:Name="shellDomestic"
Route="domestic"
Icon="paw.png">
<ShellContent Route="cats" x:Name="shellCats"
Style="{StaticResource DomesticShell}"
Title="Cats"
Icon="cat.png"
ContentTemplate="{DataTemplate views:CatsPage}" />
<ShellContent Route="dogs" x:Name="shellDogs"
Style="{StaticResource DomesticShell}"
Title="Dogs"
Icon="dog.png"
ContentTemplate="{DataTemplate views:DogsPage}" />
</Tab>
<ShellContent Route="monkeys" x:Name="shellMonkeys"
Style="{StaticResource MonkeysShell}"
Title="Monkeys"
Icon="monkey.png"
ContentTemplate="{DataTemplate views:MonkeysPage}" />
<ShellContent Route="elephants" x:Name="shellElephants"
Style="{StaticResource ElephantsShell}"
Title="Elephants"
Icon="elephant.png"
ContentTemplate="{DataTemplate views:ElephantsPage}" />
<ShellContent Route="bears" x:Name="shellBears"
Style="{StaticResource BearsShell}"
Title="Bears"
Icon="bear.png"
ContentTemplate="{DataTemplate views:BearsPage}" />
</FlyoutItem>
In the code behind I can successfully set the default startup page.
For example, the dogs page:
shellAnimals.CurrentItem.CurrentItem = shellDogs;
But on the bears page:
// This works, but I don't like the indexing. Will give trouble on dynamic menu's
shellAnimals.CurrentItem = shellAnimals.Items[3];
// This doesn't work, but I don't understand why. It gives a null reference exception in the Xamarin Shell code, on the PushAsync
shellAnimals.CurrentItem = shellBears;
// This shows the bears page at startup, but when I click on an item in the listview, I also get a null reference error
CurrentItem = shellBears;
Why doesn't the second/third snippet work?
How can I set the default page to the bears page without using the indexer?
Can't find anything on this subject except Set default tab on Xamarin Forms Shell but that doesn't answer it.
You can set the current page in XAML, as per the documentation Set-the-current-flyoutitem. This would then mean your Shell tag, you would add `CurrentItem="{x:Reference shellBears}"1.
Additionally, I see in your second code behind snippet you are missing another .CurrentItem step when compared to the dogs example that you say works. Can you try shellAnimals.CurrentItem.CurrentItem = shellBears;
Why doesn't the second/third snippet work?
The second snippet:
shellAnimals.CurrentItem is a variable which type is ShellSection while your shellBears is kind of ShellContent.
The third snippet:
When you use CurrentItem directly in the shell class, that is Shell.CurrentItem and it's a ShellItem.
How can I set the default page to the bears page without using the
indexer?
In your Xaml:
<ShellSection x:Name="shellBears" Title="Bears"
Icon="bear.png">
<ShellContent Route="bears"
Style="{StaticResource BearsShell}"
ContentTemplate="{DataTemplate views:BearsPage}" />
</ShellSection >
And in code behind:
shellAnimals.CurrentItem = shellBears;
Related
i need to show in a label the Title property of a ShellContent. Is it possible? Thanks!
This is the code for the item menu
<FlyoutItem Title="My Places" Icon="icon_places.png">
<ShellContent Route="MapsPage" ContentTemplate="{DataTemplate local:MapsPage}" Title="Ayto" />
</FlyoutItem>
I tried with await DisplayAlert("Alert" . . .):
ShellContent.TitleProperty.PropertyName
and
ShellContent.Title
unsuccessfully
You can add the x:name attribute to the xaml page.
Use name.title in the background code to get the name.
Here is the xaml code:
<FlyoutItem Title="My Places" Icon="icon_places.png">
<ShellContent Title="Monkeys"
Icon="monkey.png"
ContentTemplate="{DataTemplate views:MainPage}" x:Name="mytest"/>
</FlyoutItem>
Here is the background code:
var titile = mytest.Title;
In this example,
enter image description here
click the item item to jump to the detail page,
enter image description here
but I don't want to display the bottom tab on the total page. I used [shell. Current. Navigation. Pushmodal async], but the result is that it doesn't realize the top navigation bar. Now I want to ask how to jump to display the top navigation bar without displaying the bottom tab
some code:
<FlyoutItem Route="animals"
FlyoutDisplayOptions="AsMultipleItems">
<Tab Title="Domestic"
Route="domestic"
Icon="paw.png">
<ShellContent Route="cats"
Style="{StaticResource DomesticShell}"
Title="Cats"
Icon="cat.png"
ContentTemplate="{DataTemplate views:CatsPage}" />
<ShellContent Route="dogs"
Style="{StaticResource DomesticShell}"
Title="Dogs"
Icon="dog.png"
ContentTemplate="{DataTemplate views:DogsPage}" />
</Tab>
<ShellContent Route="monkeys"
Style="{StaticResource MonkeysShell}"
Title="Monkeys"
Icon="monkey.png"
ContentTemplate="{DataTemplate views:MonkeysPage}" />
<ShellContent Route="elephants"
Style="{StaticResource ElephantsShell}"
Title="Elephants"
Icon="elephant.png"
ContentTemplate="{DataTemplate views:ElephantsPage}" />
<ShellContent Route="bears"
Style="{StaticResource BearsShell}"
Title="Bears"
Icon="bear.png"
ContentTemplate="{DataTemplate views:BearsPage}" />
</FlyoutItem>
and
await Shell.Current.GoToAsync($"//animals/monkeys/monkeydetails?name={monkeyName}");
Now I want to ask how to jump to display the top navigation bar
without displaying the bottom tab
The simplest method is to add following code for your detail page:
Shell.TabBarIsVisible="False"
The whole sample code is:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xaminals.Views.ElephantDetailPage"
Shell.TabBarIsVisible="False"
Title="Elephant Details">
<ScrollView>
<StackLayout Margin="20">
<!--other code-->
</StackLayout>
</ScrollView>
</ContentPage>
Is it possible to dynamically bind the Key of a StaticResource in Xamarin.Forms XAML?
I am trying to build a Shell Flyout menu and would like to use Font Icons
Here is the Item Template
<Shell.ItemTemplate>
<DataTemplate>
<ContentView>
<StackLayout Orientation="Horizontal">
<Label Text="{StaticResource *key={Binding Icon}*}"></Label>
<Label Text="{Binding Title}"></Label>
</StackLayout>
</ContentView>
</DataTemplate>
</Shell.ItemTemplate>
The menu items are created using
<FlyoutItem Title="HOME" FlyoutDisplayOptions="AsMultipleItems">
<ShellContent Title="Categories & Cards" Icon="fa-th-large">
</ShellContent>
<ShellContent Title="To-Dos" Icon="fa-tasks">
</ShellContent>
</FlyoutItem>
I am trying to avoid creating a converter
I am using the technique outlined here
https://montemagno.com/xamarin-forms-custom-fonts-everywhere/
I didn't know your settings but for me you'll do better to go with the Dynamic Ressoures.
The resource:
<x:String x:Key="Icon"></x:String>
The Xaml:
<Label Text="{DynamicResource Icon}"></Label>
And then in the ViewModel, you can play with the change of the text.
App.Current.Resources["Icon"] = YourNewValueHere;
i have a parent shell tabbed page. i want to be able to navigate from one of the contentPage of this parent page to another shell tabbed page.
Navigation can be performed by specifying a valid absolute URI as an argument to the GoToAsync method: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#perform-navigation
On OnItemSelected method, invoke the code below.
await (App.Current.MainPage as Xamarin.Forms.Shell).GoToAsync("//tabbar/tab/about", true);
AppShell.xaml: Set the route.
<TabBar Route="tabbar">
<Tab Title="Browse" Icon="tab_feed.png">
<ShellContent ContentTemplate="{DataTemplate local:ItemsPage}" />
</Tab>
<Tab Title="About" Icon="tab_about.png" Route="tab">
<ShellContent Route="about" ContentTemplate="{DataTemplate local:AboutPage}" />
</Tab>
</TabBar>
How to set the default selected Tab inside the tab bar on Xamarin forms shell?
<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.NavigationShell"
xmlns:pages="clr-namespace:TestApp.Pages"
xmlns:pageModels="clr-namespace:TestApp.PageModels">
<TabBar Route="testapp">
<Tab Title="Saved" Icon="tabDashboard" Route="dashboard"><ShellContent ContentTemplate="{DataTemplate pages:DashBoardPage}"/></Tab>
<Tab Title="Calendar" Icon="tabCalendar" Route="calendar"><ShellContent ContentTemplate="{DataTemplate pages:CalendarPage}"/></Tab>
<Tab Title="Search" Icon="tabSearch" Route="search"><ShellContent ContentTemplate="{DataTemplate pages:SearchPage}"/></Tab>
<Tab Title="Support" Icon="tabSupport" Route="support"><ShellContent ContentTemplate="{DataTemplate pages:SupportPage}"/></Tab>
<Tab Title="Profile" Icon="tabAccount" Route="account"><ShellContent ContentTemplate="{DataTemplate pages:AccountPage}"/></Tab>
</TabBar>
According to the Xamarin Forms documentation, the first Shell Content will be the default content on the screen. However; I'm trying to set the "Search" page as the default tab rather than "Saved".
I tried to set Tab Index - no luck
I also tried to call routing on onAppearing method of Shell but seems like Shell's on appearing method never gets fired.
I tried to navigate to Search as well:
public partial class NavigationShell : Shell
{
public NavigationShell()
{
InitializeComponent();
//Shell.Current.TabIndex = 2;
}
protected override async void OnAppearing()
{
base.OnAppearing();
//await Shell.Current.GoToAsync("testapp/search");
}
}
What could be the best solution that when app opens need to set the default tab?
Thank you.
Here, how I fix it by using the Name field attribute.
I assigned the name to the TabBar and the tab which I want to assign in the first place.
AppShell.xaml
<TabBar x:Name="main_tab_bar" Route="main">
<Tab Title="History">
<Tab.Icon>
<FontImageSource FontFamily="MaterialIconsRegular" Glyph="{x:Static Emojis:MaterialRegFont.List}"/>
</Tab.Icon>
<ShellContent ContentTemplate="{DataTemplate Views:CallLogPage}" />
</Tab>
<Tab x:Name="main_tab_bar_dial" Title="Dial" >
<Tab.Icon>
<FontImageSource FontFamily="MaterialIconsRegular" Glyph="{x:Static Emojis:MaterialRegFont.Dialpad}"/>
</Tab.Icon>
<ShellContent ContentTemplate="{DataTemplate Views:MainDialerPage}" />
</Tab>
<Tab Title="Settings">
<Tab.Icon>
<FontImageSource FontFamily="MaterialIconsRegular" Glyph="{x:Static Emojis:MaterialRegFont.Settings}"/>
</Tab.Icon>
<ShellContent ContentTemplate="{DataTemplate Views:MainSettingsPage}" />
</Tab>
</TabBar>
AppShell.cs
public AppShell() {
InitializeComponent();
Init();
}
private void Init() {
main_tab_bar.CurrentItem = main_tab_bar_dial;
}
Nevermind, I think I found a solution.
public Page Init(Shell root)
{
CurrentShell = root;
root.CurrentItem.CurrentItem = root.CurrentItem.Items[2];
return CurrentShell;
}
Becase my shell have ONLY one root item which is tabbar itself. I just get the search tab and assigned to the first child's current item and it worked.
Please post your answers if you found another way around.
Thank you.
On the constructor of your Shell class you can set the CurrentItem
in my case I have an <ShellItem> with some <tab>
public PrivateShell()
{
InitializeComponent();
this.CurrentItem.CurrentItem = homeTab; //tab defined by x:Name on XAML
}
As far as I could check it does not pre-render the first shellcontent in case you're using ContentTemplate pattern