2016-12-05 77 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