Skip to content

Commit

Permalink
Resolved null ref when changing control height within a FlexLayout. A…
Browse files Browse the repository at this point in the history
…lso simplified the code slightly.
  • Loading branch information
Liam2349 committed Jan 17, 2019
1 parent 4fe7f92 commit 50701ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
17 changes: 5 additions & 12 deletions Xamarin.Forms.Core/FlexLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,18 @@ void RemoveChild(View view)

void OnChildPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if ( e.PropertyName == WidthRequestProperty.PropertyName
|| e.PropertyName == HeightRequestProperty.PropertyName) {
var item = (sender as FlexLayout)?._root ?? GetFlexItem((BindableObject)sender);
var item = (sender as FlexLayout)?._root ?? GetFlexItem((BindableObject)sender);
if (item == null)
return;

if (e.PropertyName == WidthRequestProperty.PropertyName || e.PropertyName == HeightRequestProperty.PropertyName) {
item.Width = ((View)sender).WidthRequest < 0 ? float.NaN : (float)((View)sender).WidthRequest;
item.Height = ((View)sender).HeightRequest < 0 ? float.NaN : (float)((View)sender).HeightRequest;
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
return;
}

if (e.PropertyName == MarginProperty.PropertyName) {
var item = (sender as FlexLayout)?._root ?? GetFlexItem((BindableObject)sender);
if (item == null)
return;
var margin = (Thickness)((View)sender).GetValue(MarginProperty);
item.MarginLeft = (float)margin.Left;
item.MarginTop = (float)margin.Top;
Expand All @@ -364,9 +363,6 @@ void OnChildPropertyChanged(object sender, PropertyChangedEventArgs e)
}

if (e.PropertyName == PaddingProperty.PropertyName) {
var item = (sender as FlexLayout)?._root ?? GetFlexItem((BindableObject)sender);
if (item == null)
return;
var padding = (Thickness)((View)sender).GetValue(PaddingProperty);
item.PaddingLeft = (float)padding.Left;
item.PaddingTop = (float)padding.Top;
Expand All @@ -377,9 +373,6 @@ void OnChildPropertyChanged(object sender, PropertyChangedEventArgs e)
}

if (e.PropertyName == IsVisibleProperty.PropertyName) {
var item = (sender as FlexLayout)?._root ?? GetFlexItem((BindableObject)sender);
if (item == null)
return;
item.IsVisible = (bool)((View)sender).GetValue(IsVisibleProperty);
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
return;
Expand Down
12 changes: 10 additions & 2 deletions Xamarin.Forms.Core/StackLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ protected override void LayoutChildren(double x, double y, double width, double
for (var i = 0; i < LogicalChildrenInternal.Count; i++)
{
var child = (View)LogicalChildrenInternal[i];
try { if (child.IsVisible) LayoutChildIntoBoundingRegion(child, _layoutInformation.Plots[i], _layoutInformation.Requests[i]); }
catch (NullReferenceException) { return; }
try
{
if (child.IsVisible)
LayoutChildIntoBoundingRegion(child, _layoutInformation.Plots[i], _layoutInformation.Requests[i]);
}
catch (NullReferenceException)
{
if (_layoutInformation.Plots != null || _layoutInformation.Requests != null) throw;
return;
}
}
}

Expand Down

0 comments on commit 50701ae

Please sign in to comment.