如何使用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