【问题标题】:Xamarin Forms Dynamically add shell itemsXamarin Forms 动态添加 shell 项
【发布时间】:2020-04-21 11:40:00
【问题描述】:

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

<?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,但我不知道该怎么做。

有什么建议吗?

【问题讨论】:

  • 就像你给了一个名字x:Name="MainShell"一样,你为什么不对你想在C#中访问的ShellContent项做同样的事情呢?

标签: c# xamarin xamarin.forms xamarin.android xamarin.ios


【解决方案1】:

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

如果您想在 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);
    }
}

myshell 是 Shell 的名称。

x:Name="myshell"

结果:

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 2019-11-25
    • 2021-02-22
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    相关资源
    最近更新 更多