【发布时间】:2016-12-05 10:52:34
【问题描述】:
我有一个 Xamarin Forms PCL 项目,它由几个选项卡式页面组成。当应用程序启动时,它会检查用户是否已登录。如果未登录,则加载登录屏幕,否则加载 MainPage(创建选项卡式项目)。
我遇到的问题是,在登录屏幕上,我只想在他们成功登录后关闭此窗口,但是通过使用 PopModalAsync,页面无法获取自身的实例(导致空异常) .然后该页面就坐在那里什么也不做,直到您重新启动它并让您进入。
我需要一种关闭登录屏幕的方法,但似乎找不到关于如何做到这一点的明确建议。
App.cs:
private async void Login_Clicked(object sender, EventArgs e)
{
if ((!string.IsNullOrWhiteSpace(username.Text) && !string.IsNullOrWhiteSpace(password.Text)))
{
indicator.IsVisible = true;
indicator.IsRunning = true;
LoggedInUserInfoRoot liuir = await WebDataAccess.LoginUser(username.Text, password.Text);
try
{
if (liuir.user != null)
{
if (liuir.user.userId > 0)
{
UserInfoRepository.UpdateUserInformation(liuir.user.userId, DateTime.Now, liuir.user.userName);
await WebDataAccess.SaveSwipesToCloud();
await Navigation.PushModalAsync(new MainPage());
//var poppedPage = await Navigation.PopModalAsync();
}
else
{
DisplayAlert("Login", "Your username or password is incorrect", "Ok");
indicator.IsVisible = false;
indicator.IsRunning = false;
}
}
else
{
DisplayAlert("Login", "Your username or password is incorrect", "Ok");
indicator.IsVisible = false;
indicator.IsRunning = false;
}
}
catch (Exception ex)
{
ErrorRepository.InsertError(ex.ToString());
}
}
else
{
DisplayAlert("Login", "You must enter both a username and password", "Ok");
indicator.IsVisible = false;
indicator.IsRunning = false;
}
}
登录界面:
private async void Login_Clicked(object sender, EventArgs e)
{
if ((!string.IsNullOrWhiteSpace(username.Text) && !string.IsNullOrWhiteSpace(password.Text)))
{
indicator.IsVisible = true;
indicator.IsRunning = true;
LoggedInUserInfoRoot liuir = await WebDataAccess.LoginUser(username.Text, password.Text);
try
{
if (liuir.user != null)
{
if (liuir.user.userId > 0)
{
UserInfoRepository.UpdateUserInformation(liuir.user.userId, DateTime.Now, liuir.user.userName);
await WebDataAccess.SaveSwipesToCloud();
var poppedPage = await Navigation.PopModalAsync();
}
else
{
DisplayAlert("Login", "Your username or password is incorrect", "Ok");
indicator.IsVisible = false;
indicator.IsRunning = false;
}
}
else
{
DisplayAlert("Login", "Your username or password is incorrect", "Ok");
indicator.IsVisible = false;
indicator.IsRunning = false;
}
}
catch (Exception ex)
{
ErrorRepository.InsertError(ex.ToString());
}
}
else
{
DisplayAlert("Login", "You must enter both a username and password", "Ok");
indicator.IsVisible = false;
indicator.IsRunning = false;
}
}
主页:
public MainPage()
{
Children.Add(new Clocking());
Children.Add(new ChangePassword{ BackgroundColor = Color.FromHex("59a092") });
Children.Add(new CompanySetup { BackgroundColor = Color.FromHex("59a092") });
Children.Add(new Log { BackgroundColor = Color.FromHex("59a092") });
isuserloggedin();
}
public async void isuserloggedin()
{
bool isuserloggedin = false;
if (UserInfoRepository.UserLoggedInTime() != null)
{
if (UserInfoRepository.UserLoggedInTime() < DateTime.Now.AddMinutes(-60))
{
isuserloggedin = false;
}
else
{
isuserloggedin = true;
}
}
if (isuserloggedin == false)
{
// MainPage = new Login { BackgroundColor = Color.FromHex("59a092") };
await Navigation.PushModalAsync(new Login());
}
}
【问题讨论】:
标签: xamarin xamarin.forms