在 Xamarin.Forms shell 菜单中设置默认页面

2024-02-06

使用 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;
  1. 为什么第二个/第三个片段不起作用?
  2. 如何在不使用索引器的情况下将默认页面设置为“熊”页面?

找不到任何关于这个主题的东西,除了在 Xamarin Forms Shell 上设置默认选项卡 https://stackoverflow.com/questions/56812252/set-default-tab-on-xamarin-forms-shell但这并不能回答这个问题。


您可以根据文档在 XAML 中设置当前页面设置当前弹出项目 https://learn.microsoft.com/en-au/xamarin/xamarin-forms/app-fundamentals/shell/flyout#set-the-current-flyoutitem。这意味着你的Shell标签,您可以添加 `CurrentItem="{x:Reference shellBears}"1.

另外,我在你的第二个代码片段后面看到你缺少另一个.CurrentItem与你所说的狗的例子相比,步骤。你能试一下吗shellAnimals.CurrentItem.CurrentItem = shellBears;

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Xamarin.Forms shell 菜单中设置默认页面 的相关文章

随机推荐