C# Xamarin表单动态添加shell项
我有一个问题,我有一个shell页面,上面有几个C# Xamarin表单动态添加shell项,c#,xamarin,xamarin.forms,xamarin.android,xamarin.ios,C#,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我有一个问题,我有一个shell页面,上面有几个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"
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
,但我不知道如何才能做到这一点
有什么建议吗
只是现在我想使用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"
结果:
只是现在我想使用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"
结果:
就像你给了一个名字
x:name=“MainShell”
,为什么不对你想在C#中访问的ShellContent项目做同样的事情呢?就像你给了一个名字x:name=“MainShell”
,为什么不对你想在C#中访问的ShellContent项目做同样的事情呢?