Ste*_*eve 4 xamarin xamarin.forms
如何使用xamarin形式捕获DidReceiveMemoryWarning。
在xamarin Studio中进行调试时,我可以在应用程序输出中看到它们,但是我找不到如何捕获事件或如何查看使用了多少内存。
我尝试了AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize,但它引发了未实现的异常
有3种方法可以捕获iOS中的内存警告(或者至少这是我所知道的:)
这三种方式是:
在您的ViewController中,您可以覆盖DidReceiveMemoryWarning()
并处理那里的警告。对于Xamarin.Forms而言,这并不是真正的最佳方法,因为您没有UIViewController来覆盖这些方法,因此请转到选项2和3。
在您的中AppDelegate
,重写ReceiveMemoryWarning
方法。当iOS的内存不足时,这将被触发。您可以将此方法连接到PCL代码上具有的任何代码,也可以仅在特定于平台的项目中处理它。
public override void ReceiveMemoryWarning (UIApplication application)
{
// handle low memory warnings here
}
Run Code Online (Sandbox Code Playgroud)出现内存警告时,您可以使用iOS NotificationCentre接收通知。可以这样完成:
// Method style void Callback (NSNotification notification)
{
Console.WriteLine ("Received a notification UIApplication", notification);
}
void Setup ()
{
NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.DidReceiveMemoryWarningNotification, Callback);
}
Run Code Online (Sandbox Code Playgroud)然后,您可以将此“ CallBack”连接到PCL项目,以释放一些内存。
您也可以在模拟器上使用
硬件>>模拟内存警告
您可以在 iOS 项目中重写 DidReceiveMemoryWarning,并从那里通知 Xamarin.Forms 页面。我可以想到很多方法来实现这一目标,但这里有两个更明显的方法:
DependencyService
作为 DI 容器提供,但您可以使用您想要的容器:http ://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/ 归档时间: |
|
查看次数: |
1332 次 |
最近记录: |