使用Xamarin表单的IOS中的DidReceiveMemoryWarning

Ste*_*eve 4 xamarin xamarin.forms

如何使用xamarin形式捕获DidReceiveMemoryWarning。

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

我尝试了AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize,但它引发了未实现的异常

Has*_*iar 5

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

这三种方式是:

  1. 在您的ViewController中,您可以覆盖DidReceiveMemoryWarning()并处理那里的警告。对于Xamarin.Forms而言,这并不是真正的最佳方法,因为您没有UIViewController来覆盖这些方法,因此请转到选项2和3。

  2. 在您的中AppDelegate,重写ReceiveMemoryWarning方法。当iOS的内存不足时,这将被触发。您可以将此方法连接到PCL代码上具有的任何代码,也可以仅在特定于平台的项目中处理它。

    public override void ReceiveMemoryWarning (UIApplication application)
    {
            // handle low memory warnings here
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 出现内存警告时,您可以使用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项目,以释放一些内存。

您也可以在模拟器上使用

硬件>>模拟内存警告


Ste*_*oix 1

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