在Xamarin.Forms Shell菜单中设置默认页面 [英] Setting a default page in the Xamarin.Forms shell menu
问题描述
使用Xaminals示例,我试图设置默认的启动页面.这是来自xaml的相关代码:
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.
例如,狗页面:
shellAnimals.CurrentItem.CurrentItem = shellDogs;
但在熊市页面上:
// 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;
- 为什么第二/第三段不起作用?
- 如何在不使用索引器的情况下将默认页面设置为Bears页面?
除在Xamarin Forms上设置默认标签外,在此主题上找不到任何内容外壳,但无法回答.
推荐答案
为什么第二/第三段不起作用?
Why doesn't the second/third snippet work?
第二个片段:
shellAnimals.CurrentItem
是类型为ShellSection
的变量,而您的shellBears是ShellContent
的类型.
shellAnimals.CurrentItem
is a variable which type is ShellSection
while your shellBears is kind of ShellContent
.
第三段:
直接在shell类中使用CurrentItem
时,即Shell.CurrentItem
,它是ShellItem
.
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?
在您的Xaml中:
<ShellSection x:Name="shellBears" Title="Bears"
Icon="bear.png">
<ShellContent Route="bears"
Style="{StaticResource BearsShell}"
ContentTemplate="{DataTemplate views:BearsPage}" />
</ShellSection >
在后面的代码中:
shellAnimals.CurrentItem = shellBears;
这篇关于在Xamarin.Forms Shell菜单中设置默认页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!