Could you get the width value in your DeviceDisplay_MainDisplayInfoChanged
method?
If yes, I create a Pixel emulator, I get the width is 350 in the Portrait, 800 in the landscape.
So I write a method to change spans amount according width of devices for testing. If the width is over 400, I set the span amount to 4, other span amount is 2. As note, I add x:Name="myCollectionView"
for CollectionView in the xml layout
void ChangeSpanByDeviceWidth()
{
var width = DeviceDisplay.Current.MainDisplayInfo.Width / DeviceDisplay.Current.MainDisplayInfo.Density;
GridItemsLayout gridItemsLayout = new GridItemsLayout(ItemsLayoutOrientation.Vertical);
if (width > 400)
{
gridItemsLayout.Span = 4;
}
else
{
gridItemsLayout.Span = 2;
}
myCollectionView.ItemsLayout = gridItemsLayout;
DisplayAlert("info", width.ToString(),"OK");
}
Then this emthod will be invoked in the OnAppearing method and screen size changed method.
protected override void OnAppearing()
{
base.OnAppearing();
ChangeSpanByDeviceWidth();
}
private void DeviceDisplay_MainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
{
ChangeSpanByDeviceWidth();
}
private void myCollectionView_Loaded(object sender, EventArgs e)
{
DeviceDisplay.MainDisplayInfoChanged += DeviceDisplay_MainDisplayInfoChanged;
}