首页 > 解决方案 > 如何在后面的代码中在 xamarin.forms shell 中添加两个页面

问题描述

我想在我的 LoginShell 中动态添加两个页面(ShellContents),但LoginShell.Current始终为空?

    {
        public LoginShell(string page = null)
        {
            InitializeComponent();


            ShellItem si = new ShellItem();


            LoginShell.Current.Items.FirstOrDefault().Items.Add(new ShellContent {ContentTemplate = new DataTemplate(typeof(SignUpPage))});

        }
    }

LoginShell.Current 是一个只读属性。

更新

我为我的 StartUpShell 类实现了以下代码:

    public partial class StartUpShell : Shell
    {
        public StartUpShell(string page)
        {
            InitializeComponent();

            ShellContent content;

            if(page == nameof(SignUpPage))
            {
               content = new SignUpPage();
            }
            else if(page == nameof(LoginPinPage))
            {
                content = new LoginPinPage();
            }
            else
            {
                content = new SignUpPage();
            }

            ShellSection shellSection = new ShellSection();
            shellSection.Items.Add(new ShellContent() { Content = content });

            CurrentItem = shellSection;
        }

但是当我设置内容变量时,它会崩溃并显示一条消息: ShellContent Content should be of type Page. Title , Route D_FAULT_ShellContent4

标签: xamarinxamarin.forms

解决方案


如果您想在 C# 中执行此操作,可以尝试下面的代码。

public partial class AppShell : Xamarin.Forms.Shell
{
    public AppShell()
    {
        InitializeComponent();
        ShellSection shell_section = new ShellSection
        {
            Title = "home",
        };

        shell_section.Items.Add(new ShellContent() { Content = new ItemsPage() });

        ShellSection shell_section1 = new ShellSection
        {
            Title = "about",


        };

        shell_section1.Items.Add(new ShellContent() { Content = new AboutPage() });

        myshell.Items.Add(shell_section);
        myshell.Items.Add(shell_section1);
    }
}

x:Name="myshell"是 的名称Shell

这里正在运行 GIF。

在此处输入图像描述

更新

如果您的 LoginShell 类型是Xamarin.Forms.Shell,您想用您的需要替换当前页面,您可以使用以下代码。

    public partial class AppShell : Xamarin.Forms.Shell
    {
        public AppShell()
        {
            InitializeComponent();



            ShellSection shell_section1 = new ShellSection
            {
                Title = "Page1",


            };

            shell_section1.Items.Add(new ShellContent() { Content = new Page1() });

            CurrentItem = shell_section1;



        }
    }

这是运行截图。

在此处输入图像描述

如果要隐藏导航栏。您可以Shell.NavBarIsVisible="false"在您的 ContentPage 中添加。这是关于在 page1 中添加它的代码。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
            Shell.NavBarIsVisible="false"
             x:Class="App18.Views.Page1">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Welcome to Xamarin.Forms!"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

在此处输入图像描述


推荐阅读