如何拦截硬件栏后退按钮在Xamarin Forms中单击Android?

时间:2016-08-22 06:24:25

标签: c#-4.0 xamarin.forms

我想在我的Xamarin Forms Portable应用程序中截取硬件背面,首先我有登录页面,你必须登录并且它将导航到我的主页2 ..在我的主页2之后,当我点击我的硬件后退按钮...它进入我的登录页面或我之前打开的页面..我想要预防它。任何人都可以解决这个问题.. 这是我的登录页面

    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");
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

用您的Homepage2替换App的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());
}