2016-12-05 32 views
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()); 
    } 

} 

回答

-1

我的選項卡頁面

public class MyTabbedPage:TabbedPage 

不要忘記這個

await Navigation.PushModalAsync(new NavigationPage(new MyTabbedPage())); 

把你的網頁,我有更重要的是你的代碼是這樣的

BackgroundColor = Color.FromRgb (76, 108, 139) 
Device.OnPlatform(() => 
{ 
    this.Padding = new Thickness(0, 30, 0, 0); 
}); 

and i add my pages with this function 
void AddPage(string title, params View[] views) 
    { 
     var content = new StackLayout(); 
     foreach (var view in views) 
      content.Children.Add(view); 
     this.Children.Add(new ContentPage 
     { 
      Content = content, 
      Title = title 
     }); 
    } 

希望它可以幫助

+0

有了這個,我得到一個錯誤:system.missingMethodException而:方法未找到'Android.Support.V4.Widget.DrawerLayout.AddDrawerListener'。 – connersz

+0

怎麼.. ..我有一個標籤頁,我用這個代碼..它和它的作品..你如何創建你的標籤頁? –

+0

我添加了顯示如何創建標籤頁的MainPage代碼。 – connersz