0
我有一個Xamarin Forms PCL項目,由幾個標籤頁面組成。當應用程序啓動時,它會檢查用戶是否已登錄。如果它們不是,則會加載登錄屏幕,否則會加載MainPage(它會創建標籤項)。如何關閉Xamarin表單中的當前窗口?
我遇到的問題是,在登錄屏幕上,我只想在他們成功登錄後關閉此窗口,但通過使用PopModalAsync時,頁面無法獲取其本身的實例(導致產生null異常)。該頁面然後就坐在那裏無所事事,直到你重新啓動它,它可以讓你進入。
我需要一種方法來關閉登錄屏幕,但似乎無法找到一個明確的建議,我該如何做到這一點。
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;
}
}
的MainPage:
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());
}
}
有了這個,我得到一個錯誤:system.missingMethodException而:方法未找到'Android.Support.V4.Widget.DrawerLayout.AddDrawerListener'。 – connersz
怎麼.. ..我有一個標籤頁,我用這個代碼..它和它的作品..你如何創建你的標籤頁? –
我添加了顯示如何創建標籤頁的MainPage代碼。 – connersz