lonelydev101
lonelydev101

Reputation: 1911

How to add two pages in xamarin.forms shell in code behind

I want to add dynamically two pages (ShellContents) into my LoginShell, but LoginShell.Current is always null?

    {
        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 is a read-only property.

Update

I implemented following code for my StartUpShell class:

    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;
        }

but on when I set content variable it crashes with a message: ShellContent Content should be of type Page. Title , Route D_FAULT_ShellContent4

Upvotes: 1

Views: 2823

Answers (1)

Leon Lu
Leon Lu

Reputation: 9274

If you want to do this in C#, you could try the code below.

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" is the name of Shell.

Here is running GIF.

enter image description here

update

If you type of LoginShell is Xamarin.Forms.Shell, You want to replace current page with your needs, you can use following code.

    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;



        }
    }

Here is running screenshot.

enter image description here

If you want to hide the navigation bar. you can add Shell.NavBarIsVisible="false" in your ContentPage. Here is code about adding it in 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>

enter image description here

Upvotes: 7

Related Questions