2016-08-22 62 views
0

我想在我的Xamarin Forms Portable應用程序中截取硬件背部按鈕,首先,我有登錄頁面,您必須登錄,它將導航到我的主頁2 ..進入我的主頁2後,當我單擊硬件後退按鈕時。 ..它進入我的登錄頁面或我之前打開的頁面。我想阻止它。任何人都可以請我解決這個問題.. 這裏是我的登錄頁面如何攔截硬件Bar Back Button在Xamarin Forms中的Android中單擊?

public LoginPage() 
    { 
     InitializeComponent(); 
    } 
    public async void LoginBtn_Clicked(object sender, EventArgs e) 
    { 
     if (UserName.Text == null || password1.Text == null) 
     { 
      DisplayAlert("Alert ! ", "Please Enter UserName Or/Password!", "OK"); 
     } 
     else 
     { 
      string uname = UserName.Text; 
      string pswd = password1.Text; 
      LoginService objservice = new LoginService(); 
      LoginTokenModel result = await objservice.GetLogin(uname, pswd); 
      LoginTokenModel logintokenmodel = new LoginTokenModel(); 
      logintokenmodel.User_Id = result.User_Id; 
      var Login_Token = result.Login_Token; 
      int user_Id = result.User_Id; 

      if (uname == result.User_Nmae) 
      { 
       // HomePage2 HOMEPge = new HomePage2(); 
       await Navigation.PushModalAsync(new HomePage2(user_Id)); 
      } 
      else 
      { 
       DisplayAlert("Alert ! ", "Invalid Credentials!", "OK"); 
      } 

     } 
    } 

回答

0

與Homepage2替換應用的的MainPage。 ,因爲您不想在NavigationStack中使用登錄頁面。

這是我成功的登錄操作:

if (uname == result.User_Nmae) 
{ 
    App.Current.MainPage = new HomePage2(user_Id); 
} 

此外,您還可以保持這個user_Id存儲第二次當用戶在應用程序屬性再次進入,並檢查USER_ID存在與否,如果它不只是導航到Homepage2

Application.Current.Properties["id"] = user_Id; 

在App.cs在onStart

if (Application.Current.Properties.ContainsKey("id")) 
{ 
    var user_Id = Application.Current.Properties["id"] as string; 
    MainPage.Navigation.PushModalAsync(new Views.Homepage2(user_Id)); 
} 
else 
{ 
    MainPage.Navigation.PushModalAsync(new Views.Login()); 
}