Skip to content

Commit

Permalink
Fix crash using SwipeView inside CollectionView on Windows (#6220)
Browse files Browse the repository at this point in the history
Co-authored-by: redth <[email protected]>
  • Loading branch information
jsuarezruiz and Redth authored Apr 25, 2022
1 parent 7985243 commit c785ec6
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ public double ItemWidth
public static readonly DependencyProperty ItemSpacingProperty = DependencyProperty.Register(
nameof(ItemSpacing), typeof(Thickness), typeof(ItemContentControl),
new PropertyMetadata(default(Thickness)));

public Thickness ItemSpacing
{
get => (Thickness)GetValue(ItemSpacingProperty);
set => SetValue(ItemSpacingProperty, value);
}

protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
Expand Down Expand Up @@ -185,7 +185,7 @@ internal void Realize()
itemsView?.AddLogicalChild(_visualElement);
}

void SetNativeStateConsistent(VisualElement visualElement)
void SetNativeStateConsistent(VisualElement visualElement)
{
visualElement.IsPlatformStateConsistent = true;

Expand Down Expand Up @@ -227,6 +227,7 @@ protected override WSize MeasureOverride(WSize availableSize)
var frameworkElement = Content as FrameworkElement;

var formsElement = _renderer.VirtualView as VisualElement;

if (ItemHeight != default || ItemWidth != default)
{
formsElement.Layout(new Rect(0, 0, ItemWidth, ItemHeight));
Expand All @@ -235,7 +236,8 @@ protected override WSize MeasureOverride(WSize availableSize)

frameworkElement.Margin = WinUIHelpers.CreateThickness(ItemSpacing.Left, ItemSpacing.Top, ItemSpacing.Right, ItemSpacing.Bottom);

frameworkElement.Measure(wsize);
if (CanMeasureContent(frameworkElement))
frameworkElement.Measure(wsize);

return base.MeasureOverride(wsize);
}
Expand All @@ -251,7 +253,8 @@ protected override WSize MeasureOverride(WSize availableSize)

var wsize = new WSize(width, height);

frameworkElement.Measure(wsize);
if (CanMeasureContent(frameworkElement))
frameworkElement.Measure(wsize);

return base.MeasureOverride(wsize);
}
Expand All @@ -266,5 +269,14 @@ double ClampInfinity(double value)
{
return double.IsInfinity(value) ? 0 : value;
}

bool CanMeasureContent(FrameworkElement frameworkElement)
{
// Measure the SwipeControl before has loaded causes a crash on the first layout pass
if (frameworkElement is SwipeControl swipeControl && !swipeControl.IsLoaded)
return false;

return true;
}
}
}

0 comments on commit c785ec6

Please sign in to comment.