我的 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 MainActivity
from 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/