如何使用xamarin表单捕捉DidReceiveMemoryWarning?DidReceiveMemoryWarning在iOS中使用Xamarin表格
在xamarin studio中调试时,我可以在应用程序输出中看到它们,但我无法找到如何捕获事件或如何查看使用了多少内存。
我试图AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize但它抛出一个没有实现的异常
如何使用xamarin表单捕捉DidReceiveMemoryWarning?DidReceiveMemoryWarning在iOS中使用Xamarin表格
在xamarin studio中调试时,我可以在应用程序输出中看到它们,但我无法找到如何捕获事件或如何查看使用了多少内存。
我试图AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize但它抛出一个没有实现的异常
你可以在你重写DidReceiveMemoryWarning的iOS项目,并从那里通知Xamarin.Forms页面。我可以想到很多方法来实现这一点,但这里有两个更明显的:
DependencyService
作为DI容器,但您可以使用您想要的那个:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/是的,如果您创建自己的UIViewController,则可以覆盖DidReceiveMemoryWarning,但UIViewController是通过App为您生成的。表单中的GetMainPage().CreateViewController()方法,这就是为什么我不确定如何覆盖它。 – Steve 2014-09-03 09:58:25
有3种方法来捕获内存警告iOS中(或者至少这是我知道的:)
这三种方式是:
在您的ViewControllers中,您可以覆盖DidReceiveMemoryWarning()
并处理那里的警告。这是不是真的Xamarin.Forms的最佳途径,你不必UIViewController中覆盖这些方法,让移动到选项2和3
在你AppDelegate
,覆盖ReceiveMemoryWarning
方法。这会在iOS内存不足时触发。您可以将此方法连接到PCL代码中的任何代码,或者只在特定于平台的项目中处理它。
public override void ReceiveMemoryWarning (UIApplication application)
{
// handle low memory warnings here
}
当存在内存警告时,您可以使用iOS NotificationCentre接收通知。这可能就像这样:
// Method style void Callback (NSNotification notification)
{
Console.WriteLine ("Received a notification UIApplication", notification);
}
void Setup()
{
NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.DidReceiveMemoryWarningNotification, Callback);
}
然后,您可以导线这个“回调”到你的PCL项目腾出一些内存。
您还可以测试此使用
硬件模拟器>>模拟内存警告
http://iosapi.xamarin.com/index.aspx?link=M%3AMonoTouch。 UIKit.UIViewController.DidReceiveMemoryWarning – 2014-08-28 16:59:51
你好我已经看到,我只是无法看到如何做到这一点在Xamarin形式作为rootviewcontroller自动生成。 window.RootViewController = App.GetMainPage().CreateViewController(); – Steve 2014-08-29 09:51:58
我会从Xamarin.Forms.IOS.Platform复制代码,添加内存处理程序并将我自己的副本作为根视图控制器。我知道这很不方便,但它应该可以帮助您排除故障,然后您可以使用修复程序恢复到当前代码 – 2014-08-29 15:42:23