如何在后面的代码中在 xamarin.forms shell 中添加两个页面

Ste*_*BEG 1 xamarin xamarin.forms

我想动态地将两个页面 (ShellContents) 添加到我的 LoginShell 中,但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))});

        }
    }
Run Code Online (Sandbox Code Playgroud)

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;
        }
Run Code Online (Sandbox Code Playgroud)

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

Leo*_*SFT 5

如果你想在 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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;



        }
    }
Run Code Online (Sandbox Code Playgroud)

这是运行截图。

在此处输入图片说明

如果要隐藏导航栏。您可以添加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>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明