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

Jas*_*per 6 xamarin.forms xamarin.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>
Run Code Online (Sandbox Code Playgroud)

在后面的代码中我可以成功设置默认启动页面。

例如,狗页面:

shellAnimals.CurrentItem.CurrentItem = shellDogs;
Run Code Online (Sandbox Code Playgroud)

但在熊页面上:

// 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;
Run Code Online (Sandbox Code Playgroud)
  1. 为什么第二个/第三个片段不起作用?
  2. 如何在不使用索引器的情况下将默认页面设置为空头页面?

除了在 Xamarin Forms Shell 上设置默认选项卡之外,找不到关于此主题的任何内容,但这并没有回答。

Jac*_*Hua 2

为什么第二个/第三个片段不起作用?

第二个片段:

shellAnimals.CurrentItem是一个变量,其类型为 ,ShellSection而您的 shellBears 为ShellContent

第三个片段:

当您CurrentItem直接在 shell 类中使用时,那就是Shell.CurrentItem它是一个ShellItem.

如何在不使用索引器的情况下将默认页面设置为“熊”页面?

在你的 Xaml 中:

    <ShellSection x:Name="shellBears" Title="Bears"
                      Icon="bear.png">
        <ShellContent Route="bears"
                      Style="{StaticResource BearsShell}"                                                   
                      ContentTemplate="{DataTemplate views:BearsPage}" />
    </ShellSection >
Run Code Online (Sandbox Code Playgroud)

并在后面的代码中:

    shellAnimals.CurrentItem = shellBears;
Run Code Online (Sandbox Code Playgroud)