c# - Xamarin 表单外壳与登录屏幕不导航到主页内容混淆
问题描述
所以下面是我的 xamrain forms shell,我在使用标签栏时遇到了问题。我有一个登录页面,所以在我的 App.xaml.cs 中有以下内容
public App()
{
Syncfusion.Licensing.SyncfusionLicenseProvider
.RegisterLicense("");//remove license for security
DevExpress.XamarinForms.Editors.Initializer.Init();
DevExpress.XamarinForms.DataGrid.Initializer.Init();
InitializeComponent();
MainPage = new NavigationPage(new LoginPage());
}
如您所见,我将主页放入登录页面,但是当我在添加页面中单击下面的主页按钮时,主页不会导航回主屏幕。
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:THEHOCKEYLAB.Views"
Title="THEHOCKEYLAB"
x:Class="THEHOCKEYLAB.AppShell">
<!--
The overall app visual hierarchy is defined here, along with navigation.
https://docs.microsoft.com/xamarin/xamarin-forms/app-fundamentals/shell/
-->
<Shell.Resources>
<ResourceDictionary>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
<TabBar>
<ShellContent Title="Menu" Icon="home.png" ContentTemplate="{DataTemplate local:AdminMenuPage}" />
<ShellContent Title="Settings" Icon="settings.png" ContentTemplate="{DataTemplate local:SettingsPage}" />
</TabBar>
<!--
If you would like to navigate to this content you can do so by calling
await Shell.Current.GoToAsync("//LoginPage");
-->
<TabBar>
<ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
</TabBar>
所以例如在这里我有一个问题
private async void btnAddStudent_Clicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync("StudentsPage");
}
当我尝试从该页面返回时,单击主页没有任何问题,有人可以向我解释为什么会这样吗?
解决方案
有两种方法可以实现这一点。
将 LoginPage 包含到 AppShell 中
设置
AppShell
为.MainPage
_App
Tabbar
在 AppShell 中放置两个,并放置在LoginPage
第一比,并为两者HomePage
设置不同。Route
Tabbar
<TabBar Route="Login"> <ShellContent ContentTemplate="{DataTemplate local:LoginPage}" /> </TabBar> <TabBar Route="Home"> <ShellContent Title="Menu" Icon="home.png" ContentTemplate="{DataTemplate local:AdminMenuPage}" /> <ShellContent Title="Settings" Icon="settings.png" ContentTemplate="{DataTemplate local:SettingsPage}" /> </TabBar>
await Shell.Current.GoToAsync("//Home");
登录时调用,await Shell.Current.GoToAsync("//Login");
注销时调用。
不要在 AppShell 中包含 LoginPage
- 设置
LoginPage
为最初MainPage
。App
MainPage = new AppShell();
登录时调用,MainPage = new LoginPage();
注销时调用。
推荐阅读
- treasure-data - 宝藏数据:选择数组中的元素
- javascript - 如何在 Vuetify 的 v-autocomplete 中设置最小长度
- awk - 将 awk 命令应用于目录中的所有文件
- python - 我可以在 Python 中定义的字符上结束字符串吗?
- api - 谷歌人员 API 在 Python 中返回空/无结果
- node.js - 在 express 4 上允许 cors
- r - R比例拟合优化
- javascript - NestJS - 如何为响应设置“Access-Control-Allow-Origin”标头
- javascript - Ruby如何将值从单选按钮传递到控制器方法
- html - bootstrap 4 响应式表单,包含带有图像的两列部分