Xamarin.Forms Android 应用程序在启动外部事件后重新启动

标签 xamarin xamarin.android xamarin.forms

我的 Xamarin.Forms Android 应用程序中的一个功能允许用户从图库中选择一张照片(它是 XLabs 的 IMediaPicker 自定义控件,它显示 native 图库事件,就像您一样在任何其他常规 Xamarin.Android 应用程序中执行)。

The problem is that sometimes the device gets low on memory (again, normal stuff) and when the image is chosen, the MainActivityfrom XF is restarted (as expected) and the app starts in the主页,而不是用户之前所在的页面。

我如何在 Xamarin.Forms 中处理这种情况?例如,从用户在打开图库事件之前所在的页面继续。

最佳答案

您可以尝试通过覆盖 OnSleep method 来保存应用程序的状态.

Xamarin Forms Samples 中有一个实现示例.特别是检查 OnSleep 和 OnResume:

protected override void OnSleep()
{
    Debug.WriteLine ("OnSleep saving ResumeAtTodoId = " + ResumeAtTodoId);
    // the app should keep updating this value, to
    // keep the "state" in case of a sleep/resume
    Properties ["ResumeAtTodoId"] = ResumeAtTodoId;
}

protected override void OnResume()
{
    Debug.WriteLine ("OnResume");
    if (Properties.ContainsKey ("ResumeAtTodoId")) {
        var rati = Properties ["ResumeAtTodoId"].ToString();

        Debug.WriteLine ("   rati="+rati);
        if (!String.IsNullOrEmpty (rati)) {
            Debug.WriteLine ("   rati = " + rati);
            ResumeAtTodoId = int.Parse (rati);

            if (ResumeAtTodoId >= 0) {
                var todoPage = new TodoItemPage ();
                todoPage.BindingContext = Database.GetItem (ResumeAtTodoId);
                MainPage.Navigation.PushAsync (
                    todoPage, false); // no animation
            }
        }
    }
}

关于Xamarin.Forms Android 应用程序在启动外部事件后重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36681850/

相关文章:

c# - 如何更改 Xamarin.Forms 中标签的字体系列?

visual-studio - Visual Studio 和 Xamarin : Unable to find packages

ios - 将来自麦克风的音频录制为 .wav 格式并使用 Xamarin.ios 中的 AudioUnit 进行播放

c# - 在 Xamarin Forms 中添加按钮的最佳方式是什么?

Xamarin Forms - 将构造函数添加到 MasterDetailPage.Master

linq - 通过MvvmCross在SQLite插件上使用多个字段的linq查询“orderby”不起作用

c# - 适用于 Android 的 Datepicker DialogFragment 单声道

android - 如何在我的应用程序不崩溃的情况下传递 DialogInterface OnDismissListener?

ios - 工具栏项目未以 xamarin 形式显示

c# - 从 NuGet 安装 Xamarin.Auth 时出错