xamarin - 如何在后面的代码中在 xamarin.forms shell 中添加两个页面
问题描述
我想在我的 LoginShell 中动态添加两个页面(ShellContents),但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
解决方案
如果您想在 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。
更新
如果您的 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;
}
}
这是运行截图。
如果要隐藏导航栏。您可以Shell.NavBarIsVisible="false"
在您的 ContentPage 中添加。这是关于在 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>
推荐阅读
- beautifulsoup - beautifulsoup:获取两个不同标签之间的文本(包括 html 标签)(
- angular - 如何使用 RxJS 在 Angular10 应用程序中触发事件?
- push-notification - 如何减少 amp-web-push 下面的空白?
- git - 如果我进行壁球合并,是否必须从分支提交历史记录中清除密码?
- vue.js - Vue 3 通过双向绑定将反应对象传递给组件
- java - 在 intellij 的本地 websphere 上部署 ear 时出错
- amazon-cloudwatch - 具有过滤模式的 AWS Secrets Manager 的事件模式
- r - 尝试解决 keras 中的数据维度不匹配问题
- bash - 使用 sed 从 XML 文件中提取值
- javascript - 在快照 Firestore JS 中返回文档 ID