gpt4 book ai didi

c# - Xamarin.Forms 选项卡式页面和注销

转载 作者:行者123 更新时间:2023-11-30 15:56:49 25 4
gpt4 key购买 nike

我在使用 Xamarin.Forms 时遇到了一种我无法解决的奇怪行为。

应用程序的基本流程是登录页面是一个登录表单。登录时,这会将您带到一个选项卡式页面。当我使用注销功能时,它会返回登录页面,但选项卡似乎保留在原位。

登录.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TechsportiseApp.Login">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness"
iOS="20, 40, 20, 20"
Android="20, 20, 20, 20"
WinPhone="20, 20, 20, 20" />
</ContentPage.Padding>
<ContentPage.Content>
<StackLayout VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
Orientation="Vertical"
Spacing="15">
<Label Text="Email" />
<Entry x:Name="email" Text="123@test.com" />
<Label Text="Password" />
<Entry x:Name="password" Text="xxxxxx" IsPassword="true"/>
<Button x:Name="loginButton" Text="Login" Clicked="OnLogin"/>
<Button x:Name="registerButton" Text="Register Account" Clicked="OnRegister"/>
<Button x:Name="clearButton" Text="Reset" Clicked="OnClear"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

如果我从 API 获得登录成功代码,此部分会将它们移至主页。

登录.xaml.cs

//Valid response
if (response.StatusCode.ToString() == "OK")
{
Application.Current.Properties["Token"] = tokenobject.access_token;
string token = Application.Current.Properties["Token"].ToString();
Navigation.InsertPageBefore (new MainPage (), this);
await Navigation.PopAsync();

}

MainPage.xaml 看起来像这样,是一个选项卡式导航页面。

<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TechsportiseApp.MainUI"
x:Class="TechsportiseApp.MainUI.MainPage">

<local:Races />
<local:Timer />
<local:Scanning />
<NavigationPage Title="Settings">
<x:Arguments>
<local:Settings />
</x:Arguments>
</NavigationPage>
</TabbedPage>

里面的每一个页面都是Valid。设置页面有一个注销按钮。

Settings.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TechsportiseApp.MainUI.Settings"
Title="Settings">
<ContentPage.Content>
<StackLayout>
<Label Text="Settings will go here" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
<Button x:Name="logoutButton" Text="Logout" Clicked="OnLogout" VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>

使用 Settings.xaml.cs 中的注销按钮执行此操作

async void OnLogout(object sender, EventArgs e)
{
Application.Current.Properties["Token"] = "";
Navigation.InsertPageBefore (new Login(), this);
await Navigation.PopAsync();
}

但最终结果是我有登录屏幕...底部有标签栏

enter image description here

我做错了什么?我希望这样当我注销时标签页不可见。

谢谢,马特

最佳答案

设置新的 MainPage 就完成了这项工作。在登录和注销时,我使用

为应用程序设置了新的 MainPage
App.Current.MainPage = new NavigationPage(new MainPage())

代替 PopAsync/PushAsync。

关于c# - Xamarin.Forms 选项卡式页面和注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46079404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com