在Xamarin.Forms Shell菜单中设置默认页面 [英] Setting a default page in the Xamarin.Forms shell menu

查看:138
本文介绍了在Xamarin.Forms Shell菜单中设置默认页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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;

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆