Xamarin Forms 动态添加外壳项 [英] Xamarin Forms Dynamically add shell items

查看:21
本文介绍了Xamarin Forms 动态添加外壳项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我有一个带有几个 ShellContent 链接的 shell 页面,如下所示:

I have a problem I have a shell page with a few ShellContent links like this:

<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:local="clr-namespace:MyApp"
       x:Class="MyApp.SideMenuItems" BackgroundColor="#212121"
       FlyoutBackgroundColor="#212121"
       x:Name="MainShell">

    <Shell.FlyoutHeader>
        <local:SideMenuHeader />
    </Shell.FlyoutHeader>

    <Shell.TitleView>
        <Image Source="Title_Dark.png" HeightRequest="30" VerticalOptions="CenterAndExpand" />
    </Shell.TitleView>

    <Shell.ItemTemplate>
        <DataTemplate>
            <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal" Padding="30, 15, 0, 15">
                <Image Source="{Binding Icon}" HeightRequest="35" />
                <Label Text="{Binding Title}" TextColor="White" FontSize="Large" VerticalOptions="Center" HorizontalOptions="Start" />
            </StackLayout>
        </DataTemplate>
    </Shell.ItemTemplate>

    <FlyoutItem Title="SideNav"
                Shell.TabBarIsVisible="False"
                FlyoutDisplayOptions="AsMultipleItems">
        <ShellContent Title="Home" Icon="Home_Dark.png" IsTabStop="true" ContentTemplate="{DataTemplate local:HomePage}"/>
        <ShellContent Title="Search" Icon="Search_Dark.png" IsTabStop="true" ContentTemplate="{DataTemplate local:HomePage}" />
        <ShellContent Title="Settings" Icon="Settings_Dark.png" IsTabStop="true" ContentTemplate="{DataTemplate local:HomePage}" />
    </FlyoutItem>
</Shell>

现在我想使用 C# 添加 ShellContent,但我不知道如何做到这一点.

Only now I want to add ShellContent using C#, but I have no idea how I can do that.

有什么建议吗?

推荐答案

现在我想使用 C# 添加 ShellContent,但我不知道我该怎么做.

Only now I want to add ShellContent using C#, but I have no idea how I can do that.

如果你想在 C# 中做到这一点,你可以试试下面的代码.

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

myshell 是 Shell 的名称.

myshell is the name of Shell.

x:Name="myshell"

结果:

这篇关于Xamarin Forms 动态添加外壳项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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