xamarin - Application.Current.MainPage 在 OnAppearing 内工作,但不在按钮单击上
问题描述
我正面临 Xamarin Forms 的这个奇怪问题。
此代码有效:
protected override void OnAppearing()
{
base.OnAppearing();
Application.Current.MainPage = new MasterIndexPage();
}
但这不起作用:
private void BtnLogin_Clicked(object sender, EventArgs e)
{
Application.Current.MainPage = new MasterIndexPage();
}
两种方法都属于同一个视图页面。我在按钮单击事件中添加了一个断点,该事件成功命中。但是 MasterIndexPage 没有出现,视图保持在同一页面上。
还有一件事......上述行为仅在我第一次安装该应用程序时出现。如果我关闭并重新启动该应用程序,上述两个代码都有效。可能有一些非常小的东西,但我挣扎了 4 个多小时,仍然无法弄清楚。
请帮忙。
更新:共享完整的代码示例以重现该问题。
MasterIndexPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Title="Workman"
x:Class="Workman.MasterIndexPage">
<MasterDetailPage.Master>
<ContentPage Title="Menu" BackgroundColor="#FFFFFF">
<Label Text="Test Menu" />
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<ContentPage Title="Content" BackgroundColor="#FFFFFF">
<Label Text="Content Area" />
</ContentPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
MasterIndexPage.xaml.cs
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Workman
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MasterIndexPage : MasterDetailPage
{
public MasterIndexPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
}
}
}
登录页面.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"
Title="About"
x:Class="Workman.LoginPage">
<ContentPage.Content>
<StackLayout Margin="20,20,20,20">
<Button Grid.Row="8" Grid.Column="1"
x:Name="btnlogin" Clicked="BtnLogin_Clicked"
TextColor="White" FontSize="Large"
Text="Login" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
登录页面.xaml.cs
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Workman
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
}
private void BtnLogin_Clicked(object sender, EventArgs e)
{
Application.Current.MainPage = new MasterIndexPage();
//This line is hitting but not working
}
}
}
我能够用两个新创建的页面和上面的极简代码重现这个问题。但是,如前所述,该问题仅在首次运行时未安装应用程序时发生。关闭并重新启动后,它可以工作。
解决方案
您的代码在我这边运行良好。而且由于它在您重新启动后可以正常工作,因此我认为问题不是由您的代码引起的。
作为更好的设计,您可以 直接在 App.xaml.cs 中设置MainPage
as 。MasterIndexPage
登录页面可以作为模式页面出现。
public partial class MasterIndexPage : MasterDetailPage
{
bool HasLogin;
public MasterIndexPage ()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
if(!HasLogin)
{
HasLogin = true; // here just for demo , you could change it when user login scuuess
Navigation.PushModalAsync(new LoginPage());
}
}
}
并在登录页面
private void Button_Clicked(object sender, EventArgs e)
{
// when login success
Navigation.PopModalAsync();
}
推荐阅读
- node.js - 尝试将路由器中的类方法作为中间件传递时“this”未定义_express
- javascript - 如何在videojs中将纵向更改为横向模式
- r - 当我的“occu”模型(来自“未标记”包)包含多个协变量时,如何对它的值进行反向转换?
- memory - Grafana 和 Prometheus 需要多少主机资源
- angular - RXJS 'never' 主题作为泛型类型
- gradle - Gradle 不生成 Checkstyle HTML 报告
- google-cloud-platform - 操作失败:创建 Dataproc 集群时需要“compute.subnetworks.use”权限
- kibana - 如何将开放跟踪标识符 (TraceId) 集成到批处理中
- java - 带有请求正文的 HTTP POST
- node.js - 如何使用 node-fetch 访问 node.js 中的 json 对象