[英]Xamarin iOS app crashes when changing from Landscape orientation to Portrait orientation
TLDR Xamarin.Forms 5.0.0.2401 應用程序在從橫向方向切換到具有縱向方向的 collectionview 的視圖時崩潰。
語境
嗨,我有一個 Xamarin.Forms 5.0.0.2401,它在 AppDelegate.cs 中有一個默認設置,即應用程序應始終處於縱向,唯一的例外是覆蓋方法 GetSupportedInterfaceOrientations 中規定的一個視圖。
來自 AppDelegate 的代碼:
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
{
if (Shell.Current.Navigation.ModalStack.LastOrDefault() is CardPage)
{
return UIInterfaceOrientationMask.Landscape;
}
return UIInterfaceOrientationMask.Portrait;
}
但是,當我從橫向視圖導航到具有先前填充項目的集合視圖的視圖時,由於滾動應用程序崩潰。
我使用以下代碼導航:
CardPage 的視圖模型內的代碼被 AppDelegate.cs 中的代碼強制為橫向
async Task ExecuteCloseCommand() => await GoToAsync("..", true);
BaseViewModel(繼承自 CardPage)的代碼,用於處理堆棧中的導航。
protected Task GoToAsync(string uri, bool animate = false)
=> MainThread.InvokeOnMainThreadAsync(async () => await Shell.Current.GoToAsync(uri, animate));
崩潰時的錯誤消息
但是我從崩潰中得到的異常如下:
>{System.NullReferenceException: Object reference not set to an instance of an object
at Xamarin.Forms.Platform.iOS.ItemsViewController`1[TItemsView].CheckForEmptySource () [0x00028]
in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewController.cs:117
at Xamarin.Forms.Platform.iOS.ItemsViewController`1[TItemsView].NumberOfSections
(UIKit.UICollectionView collectionView) [0x00000]
in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewController.cs:256
at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSendSuper(intptr,intptr)
at UIKit.UICollectionViewLayout.PrepareLayout () [0x00023]
in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/ios/native/UIKit/UICollectionViewLayout.g.cs:496
at Xamarin.Forms.Platform.iOS.ItemsViewLayout.PrepareLayout () [0x00000]
in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewLayout.cs:415
at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args,
System.Type principalClass, System.Type delegateClass) [0x0003b]
in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:85
at <ProjectName>.iOS.Application.Main (System.String[] args) [0x00001]
in C:\Users\JesperEngdahl\source\repos\<ProjectName>\Main.cs:17 }
所以我懷疑 Xamarin.Forms.Platform.iOS 中的某些東西導致崩潰,特別是第 117 行的ItemsViewController 。它以某種方式假設 collectionview 是空的,但它不是,然后它就崩潰了。
我有點難過,不確定如何處理這種崩潰,請幫忙。
返回該頁面時,也許可以重新初始化 CollectionView。
作為測試,這是否有幫助(或至少改變症狀):
<ContentPage ...>
...
<CollectionView x:Name="theCollectionView" ... >
...
private bool secondTime;
protected override void OnAppearing()
{
base.OnAppearing();
if (secondTime)
{
// TEST: Try to force collection view to release its items.
var holdItems = theCollectionView.ItemsSource;
// change "string" as needed. Set to an empty collection of the type of your ItemsSource.
theCollectionView.ItemsSource = new List<string>();
Device.BeginInvokeOnMainThread(async () =>
{
// TEST: Long delay, to make sure page has appeared with an empty collection.
await Task.Delay(1000);
theCollectionView.ItemsSource = holdItems;
});
}
else
{
secondTime = true;
}
}
如果這有助於避免異常,那么可以探索如何在沒有長時間延遲的情況下做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.