【问题标题】:Xamarin iOS method to override to save data when app navigates to background, closes or navigates to different screenXamarin iOS 方法在应用导航到后台、关闭或导航到不同屏幕时覆盖以保存数据
【发布时间】:2019-07-01 09:20:54
【问题描述】:
我对移动应用程序开发和 xamarin iOS 非常陌生。我有一个视图控制器,它有一些 UIImageViews,我正在用图像设置 UIImageview。我想知道的是在什么时候挂上什么事件来保存图像
用户导航到不同的视图控制器
应用程序进入后台
应用已关闭
我一直在研究 xamarin 生命周期,并认为 ViewDidUnload 可能是一个不错的选择。有什么想法吗?
请原谅我的无知。
【问题讨论】:
标签:
ios
swift
xcode
xamarin
xamarin.ios
【解决方案1】:
ViewDidUnload 已弃用。
您应该使用viewWillDisappear 来保存数据/图像。
每当您导航到不同的控制器时,都会调用此方法。
但这不包括,当您的应用程序进入后台或被终止时。
UIApplication 的 applicationDidEnterBackground 会在你的应用进入后台状态后被调用。并且 applicationWillTerminate 将在您的应用终止之前被调用。
您可以在视图中添加观察者来监控这些事件。
App观察者进入后台示例:
NSObject notificationObserver = NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.applicationDidEnterBackground, SaveData);
private void SaveData(NSNotification a_notification)
{
// you can save image or any other data
}
您可以为 applicationWillTerminate 实现类似的。
【解决方案2】:
Darshana 的回答是正确的。
我想补充一下观察这些事件的不同方式:
UIApplication.Notifications.ObserveDidEnterBackground(HandleNotification);
public void HandleNotification(object sender, NSNotificationEventArgs e)
{
}