在后台按钮返回我的应用程序,当您恢复时它会再次启动 [英] button back to my app in the background and when you resume it starts again

查看:23
本文介绍了在后台按钮返回我的应用程序,当您恢复时它会再次启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Xamarin.Forms 中开发一个应用程序,在我登录到我的应用程序时尝试将主详细信息页面作为我的 MainPage 之前,我已经实现了这一点.现在我遇到的问题是,当我使用手机后面的按钮时,我的应用程序是 miimiza 并进入后台,这是我希望的行为,但是当我返回我的应用程序时,不会继续显示我的主详细信息页面,而是返回到我的登录页面.

I am developing an app in Xamarin.Forms, before I was trying to make a master detail page to become my MainPage when I logged in to my app, this I have already achieved. Now I have the problem that when I use the button behind the phone my app is miimiza and goes to the background which is the behavior I hope, but when I return to my app does not continue showing my master detail page, but returns to my LginPage.

就好像我的应用程序运行了两次,或者至少有两个 LoginPage 实例同时存在,这是因为在我的 LoginPage 中,我根据我的页面正在通过 MessaginCenter 侦听的一些消息触发了一些 DisplayAlert 并且他们是他们射了两次.

It is as if my app was running twice or at least there were two instances of LoginPage existing at the same time, this is because in my LoginPage I trigger some DisplayAlert according to some messages that my page is listening through the MessaginCenter and they are they shoot twice.

谁能告诉我如何在主详细信息页面上将相同的内容返回到我的应用程序,而不是以描述的奇怪方式重新启动?

Can someone tell me how I can return the same to my app on the master detail page and not restart in the strange way described?

登录视图.xaml.cs:

LoginView.xaml.cs:

public partial class LogonView : ContentPage
{

    LogonViewModel contexto = new LogonViewModel();

    public LogonView ()
    {
        InitializeComponent ();
        BindingContext = contexto;


        MessagingCenter.Subscribe<LogonViewModel>(this, "ErrorCredentials", async (sender) =>
        {
            await DisplayAlert("Error", "Email or password is incorrect.", "Ok");
        }
        );


    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        MessagingCenter.Unsubscribe<LogonViewModel>(this, "ErrorCredentials");

    }
}

我的视图模型的一部分:

Part of my ViewModel:

if (Loged)
    {
        App.token = token;
        Application.Current.MainPage = new RootView();
    }
    else
    {   
        MessagingCenter.Send(this, "ErrorCredentials");
    }

谢谢.

推荐答案

这意味着您必须使用 Setting Plugin 或在应用程序属性中保存数据.您必须在 App.xaml.cs 文件中添加以下代码:

It means you have to Use Setting Plugin or save data in Application properties. You have to add below code in App.xaml.cs file:

if (SettingClass.UserName == null)
                MainPage = new LoginPage();
 else
                MainPage = new MasterDetailPage();

关于设置插件,您可以参考这个链接.

For Setting Plugin you can refer this link.

这篇关于在后台按钮返回我的应用程序,当您恢复时它会再次启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆