2014-08-28 34 views
1

如何使用xamarin表单捕捉DidReceiveMemoryWarning?DidReceiveMemoryWarning在iOS中使用Xamarin表格

在xamarin studio中调试时,我可以在应用程序输出中看到它们,但我无法找到如何捕获事件或如何查看使用了多少内存。

我试图AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize但它抛出一个没有实现的异常

+0

http://iosapi.xamarin.com/index.aspx?link=M%3AMonoTouch。 UIKit.UIViewController.DidReceiveMemoryWarning – 2014-08-28 16:59:51

+0

你好我已经看到,我只是无法看到如何做到这一点在Xamarin形式作为rootviewcontroller自动生成。 window.RootViewController = App.GetMainPage().CreateViewController(); – Steve 2014-08-29 09:51:58

+0

我会从Xamarin.Forms.IOS.Platform复制代码,添加内存处理程序并将我自己的副本作为根视图控制器。我知道这很不方便,但它应该可以帮助您排除故障,然后您可以使用修复程序恢复到当前代码 – 2014-08-29 15:42:23

回答

1

你可以在你重写DidReceiveMemoryWarning的iOS项目,并从那里通知Xamarin.Forms页面。我可以想到很多方法来实现这一点,但这里有两个更明显的:

+1

是的,如果您创建自己的UIViewController,则可以覆盖DidReceiveMemoryWarning,但UIViewController是通过App为您生成的。表单中的GetMainPage().CreateViewController()方法,这就是为什么我不确定如何覆盖它。 – Steve 2014-09-03 09:58:25

2

有3种方法来捕获内存警告iOS中(或者至少这是我知道的:)

这三种方式是:

  1. 在您的ViewControllers中,您可以覆盖DidReceiveMemoryWarning()并处理那里的警告。这是不是真的Xamarin.Forms的最佳途径,你不必UIViewController中覆盖这些方法,让移动到选项2和3

  2. 在你AppDelegate,覆盖ReceiveMemoryWarning方法。这会在iOS内存不足时触发。您可以将此方法连接到PCL代码中的任何代码,或者只在特定于平台的项目中处理它。

    public override void ReceiveMemoryWarning (UIApplication application) 
    { 
         // handle low memory warnings here 
    } 
    
  3. 当存在内存警告时,您可以使用iOS NotificationCentre接收通知。这可能就像这样:

    // Method style void Callback (NSNotification notification) 
    { 
         Console.WriteLine ("Received a notification UIApplication", notification); 
    } 
    
    void Setup() 
    { 
         NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.DidReceiveMemoryWarningNotification, Callback); 
    } 
    

然后,您可以导线这个“回调”到你的PCL项目腾出一些内存。

您还可以测试此使用

硬件模拟器>>模拟内存警告

相关问题