【问题标题】:DidReceiveMemoryWarning in IOS using Xamarin FormsIOS 中的 DidReceiveMemoryWarning 使用 Xamarin 表单
【发布时间】:2016-03-02 08:20:43
【问题描述】:

如何使用 xamarin 表单捕获 DidReceiveMemoryWarning。

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

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

【问题讨论】:

  • 是的,我看到我只是无法在 Xamarin Forms 中看到如何做到这一点,因为 rootviewcontroller 是自动生成的。 window.RootViewController = App.GetMainPage().CreateViewController();
  • 我会从 Xamarin.Forms.IOS.Platform 复制代码,添加内存处理程序并将我自己的副本作为根视图控制器。我知道这很 hacky,但它应该可以帮助您排除故障,然后您可以通过适当的修复恢复到当前代码

标签: xamarin xamarin.forms


【解决方案1】:

有 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 项目以释放​​一些内存。

您也可以使用

在模拟器上进行测试

硬件 >> 模拟内存警告

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,如果您创建自己的 UIViewController,则可以覆盖 DidReceiveMemoryWarning,但 UIViewController 是通过 Forms 中的 App.GetMainPage ().CreateViewController () 方法为您生成的,这就是为什么我不确定如何覆盖它.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2012-05-22
    • 2020-08-23
    • 2019-09-24
    • 2011-06-24
    • 2011-05-29
    相关资源
    最近更新 更多