xamarin.forms - 在 Xamarin.Forms shell 菜单中设置默认页面
问题描述
使用 Xaminals 示例,我正在尝试设置默认启动页面。这是来自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>
在后面的代码中我可以成功设置默认启动页面。
例如,狗页面:
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;
- 为什么第二个/第三个片段不起作用?
- 如何在不使用索引器的情况下将默认页面设置为熊页面?
除了在 Xamarin Forms Shell 上设置默认选项卡之外,找不到关于此主题的任何内容,但这并没有回答。
解决方案
根据文档Set-the-current-flyoutitem ,您可以在 XAML 中设置当前页面。这将意味着您的Shell
标签,您将添加 `CurrentItem="{x:Reference shellBears}"1。
此外,我在您的代码片段后面的第二个代码中看到,.CurrentItem
与您说的狗示例相比,您缺少另一个步骤。你能试一下吗shellAnimals.CurrentItem.CurrentItem = shellBears;
推荐阅读
- google-sheets - 在查询谷歌表格中加入表格
- if-statement - ESP8266 上的循环多线程问题
- c# - 即使使用 await Task,C# 写入文件也会挂起 UI
- javascript - 如何阻止 prettier 将我解构的代码放到新行上?
- python - 如何将 pandas.style 与 streamlit 一起使用
- html - 如何在 Flexbox 项目之间添加“|”
- python - Python,访问另一个模块(文件)中的字典
- python - matblotlib 通过循环颜色为 matshow 设置动画
- multithreading - SLURM 节点、任务、核心和 CPU
- geojson - 使用 GetJSON,我失去了属性