Как добавить две страницы в оболочку xamarin.forms в коде позади - PullRequest
1 голос
/ 16 апреля 2020

Я хочу динамически добавить две страницы (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))});

        }
    }

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

1 Ответ

3 голосов
/ 16 апреля 2020

Если вы хотите сделать это в 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.

enter image description here

обновление

Если вы используете тип 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;



        }
    }

Вот снимок экрана.

enter image description here

Если вы хотите скрыть панель навигации. Вы можете добавить Shell.NavBarIsVisible="false" в вашей ContentPage. Вот код для добавления его на стр. 1.

<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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...