Xamarin Application.Current.MainPage在外观上工作,但不在按钮上单击
我在Xamarin表单上遇到了一个奇怪的问题 此代码正在运行:Xamarin Application.Current.MainPage在外观上工作,但不在按钮上单击,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我在Xamarin表单上遇到了一个奇怪的问题 此代码正在运行: protected override void OnAppearing() { base.OnAppearing(); Application.Current.MainPage = new MasterIndexPage(); } 但这是行不通的: private void BtnLogin_Clicked(object sender, EventArgs e) { Application.Current.MainPage
protected override void OnAppearing()
{
base.OnAppearing();
Application.Current.MainPage = new MasterIndexPage();
}
但这是行不通的:
private void BtnLogin_Clicked(object sender, EventArgs e)
{
Application.Current.MainPage = new MasterIndexPage();
}
这两种方法属于同一个视图页。我在button click事件中添加了一个断点,该事件正在成功命中。但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>
LoginPage.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>
我能够用两个新创建的页面和上面的极简代码重现这个问题。但是,如前所述,只有在第一次运行时未安装应用程序时才会出现问题。关闭并重新启动后,它会工作。您的代码在我这方面工作正常。而且,由于它在重新启动后工作,我不认为问题是由您的代码引起的 作为更好的设计,您可以直接在App.xaml.cs中将
MainPage
设置为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());
}
}
}
和在登录页面
我无法复制您的问题。你可以分享你的样品,这样我就可以在我这边测试了。@LucasZhang MSFT补充道:“我这边的工作很好。”。您的项目和测试设备中的Xamarin.Forms版本是什么?您可以删除文件夹bin和obj,然后清理并重建您的项目。在发布此问题之前,我已经尝试删除PCL和Android项目中的obj和bin,但没有成功。我使用的Xamarin.Forms版本是4.6.0.800。我还认为这与一些环境问题有关,因为这是非常基本的,应该会起作用。谢谢你这么做。这可能是一个解决办法,但不能解决手头的问题。如果用户在没有尝试登录的情况下单击“上一步”,会发生什么情况?用户界面将返回到母版页。然后我必须再次检查HasLogin是否为真。所以会有一个无限循环。我的代码只是一个演示。您可以根据需要修改逻辑。对不起。今天真的很沮丧!:(我会尽力帮助你。这不是一个严重的问题,有一个解决办法。
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
}
}
}
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();
}