在Android上设置Xamarin.forms崩溃MainPage - System.ArgumentException
问题描述:
下面是我在Xamarin.Forms项目中的App.css。在Android上设置Xamarin.forms崩溃MainPage - System.ArgumentException
public partial class App : Application
{
//public static string access_token;
//public static string bearer;
public static new App Current;
public App()
{
InitializeComponent();
Current = this;
var isLoggedIn = Properties.ContainsKey("IsLoggedIn") ? (bool)Properties["IsLoggedIn"] : false;
if (isLoggedIn)
MainPage = new NavigationPage(new MasterPage());
else
MainPage = new NavigationPage(new LoginPage());
}
......
protected async override void OnResume()
{
base.OnResume();
}
public async void OnBackButtonPressed(object sender, EventArgs e)
{
}
public void ShowMainPage()
{
MainPage = new NavigationPage(new MasterPage());
}
public void Logout()
{
Properties["IsLoggedIn"] = false; // only gets set to 'true' on the LoginPage
MainPage = new NavigationPage(new LoginPage());
}
}
在那之后,我拍了拍注销按钮(它要求App.css我上面提到的退出功能),它显示了
System.ArgumentException
Handle must be valid.
Parameter name :instance
任何人都可以帮我吗?
快速信息: - 最小Android版本:4.0.3(API等级15) - 目标Android版本:自动 - 使用目标版本(API 25) - Xamarin的Android版本:7.3
更新:LoginPage
public partial class LoginPage : ContentPage
{
RestService service;
public LoginPage()
{
InitializeComponent();
var signInGesture = new TapGestureRecognizer();
signInGesture.Tapped += SignInGesture_Tapped;
signInButton.GestureRecognizers.Add(signInGesture);
NavigationPage.SetHasNavigationBar(this, false);
}
async void SignInGesture_Tapped(object sender, EventArgs e)
{
await signInButton.ScaleTo(0.95, 50, Easing.CubicOut);
signInButton.ScaleTo(1, 50, Easing.CubicIn);
if (string.IsNullOrEmpty(emailEntry.Text) || string.IsNullOrEmpty(passwordEntry.Text))
await DisplayAlert("Error", "Username and Password should not be empty.", "OK");
else if(!Utils.IsValidEmail(emailEntry.Text))
await DisplayAlert("Error", "Email should be in valid format.", "OK");
else
{
loadingIndicator.IsRunning = true;
var login = new Login{
grant_type = "password",
username = emailEntry.Text,
password = passwordEntry.Text
};
service = new RestService();
var response = await service.Login(login);
if (response != null)
{
if (response.status_code == System.Net.HttpStatusCode.OK)
{
App.Current.Properties["IsLoggedIn"] = true;
App.Current.ShowMainPage();
}
else
{
loadingIndicator.IsRunning = false;
await DisplayAlert(response.error, response.error_description, "OK");
}
}
else
{
loadingIndicator.IsRunning = false;
await DisplayAlert("Failed", Config.CommonErrorMsg, "OK");
}
}
}
async void ResetButton_Clicked(object sender, EventArgs e)
{
loadingIndicator.IsRunning = false;
await Navigation.PushAsync(new ForgetPasswordPage());
}
}
MasterPage:CS文件中的空构造函数,我只是附加了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" x:Class="MyTenantWorld.MasterPage" xmlns:local="clr-namespace:MyTenantWorld;assembly=MyTenantWorld">
<MasterDetailPage.Master>
<local:MenuPage x:Name="menuPage" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<local:HomePage />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
答
隐藏的菜单图标是在MasterDetailPage的详细信息页面上的NavigationPage.SetHasNavigationBar(this, false);
。