Forum Xamarin Xamarin.Forms

ObjectReferenceNullException in UWP with 2.4.0.280 when nested Layout<View> added to StackLayout

The below code causes ObjectReferenceNullException in UWP with Xamarin.Forms 2.4.0.280. The exception is not thrown with Xamarin.Forms versions greater than 3.0. But I want to keep 2.4.0.280. Is there any workarounds to avoid the exception without upgrading Xamarin.Forms. I have also attached the stacktrace details and the replication sample.

C#

public class CustomLayoutView : Layout<View>
{
        Grid grid;
        CustomLayoutViewChild child;

        public CustomLayoutView()
        {
               grid = new Grid();
               child = new CustomLayoutViewChild();
               grid.Children.Add(child);
               Children.Add(grid);
        }
        protected override void LayoutChildren(double x, double y, double width, double height)
        {
               grid.Layout(new Rectangle(x, y, width, height));
        }
 }

 public class CustomLayoutViewChild : Grid
 {
        protected override void LayoutChildren(double x, double y, double width, double height)
        {

        }
 }

XAML

    <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <local:CustomLayoutView />
     </StackLayout>

Answers

  • JohnHardmanJohnHardman GBUniversity admin
    edited September 2018

    @NavaneethaKannan - Which line throws the exception / what is in the stack trace of the exception?

    A couple of thoughts without knowing which line is throwing the exception,

    • does it make a difference if you call base.LayoutChildren from with the override in CustomLayoutViewChild?
    • does it make a difference if you add RowDefinition(s) and ColumnDefinition(s)?

    Like you, I was in no hurry to upgrade to XF 3.x, but getting Xamarin support for questions relating to 2.4.0 is problematic. I've now upgraded XF to 3.2 (along with upgrades to VS2017, various NuGets, etc) because of the support issue, but am still working on code changes resulting from that upgrade :-(

  • NavaneethaKannanNavaneethaKannan INMember ✭✭

    @JohnHardman - The exception is not thrown from the code given. It is thrown from the Xamarin.Forms.Platform.UAP dll. So I could not identify the line of code from where it is thrown. I have attached the sample and the stacktrace with the previous post. However, I have repeated the stack trace below for your convenience.

    Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.VisualElementRenderer<System.__Canon, System.__Canon>.GetDesiredSize(double widthConstraint, double heightConstraint)
    Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.Platform.Xamarin.Forms.Internals.IPlatform.GetNativeSize(Xamarin.Forms.VisualElement element, double widthConstraint, double heightConstraint)
    Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.OnSizeRequest(double widthConstraint, double heightConstraint)
    Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.OnMeasure(double widthConstraint, double heightConstraint)
    Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.GetSizeRequest(double widthConstraint, double heightConstraint)
    Xamarin.Forms.Core.dll!Xamarin.Forms.Layout.GetSizeRequest(double widthConstraint, double heightConstraint)
    Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.Measure(double widthConstraint, double heightConstraint, Xamarin.Forms.MeasureFlags flags)
    Xamarin.Forms.Core.dll!Xamarin.Forms.StackLayout.CalculateNaiveLayout(Xamarin.Forms.StackLayout.LayoutInformation layout, Xamarin.Forms.StackOrientation orientation, double x, double y, double widthConstraint, double heightConstraint)
    Xamarin.Forms.Core.dll!Xamarin.Forms.StackLayout.CalculateLayout(Xamarin.Forms.StackLayout.LayoutInformation layout, double x, double y, double widthConstraint, double heightConstraint, bool processExpanders)
    Xamarin.Forms.Core.dll!Xamarin.Forms.StackLayout.LayoutChildren(double x, double y, double width, double height)
    Xamarin.Forms.Core.dll!Xamarin.Forms.Layout.UpdateChildrenLayout()
    Xamarin.Forms.Core.dll!Xamarin.Forms.Layout.OnSizeAllocated(double width, double height)
    Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.SetSize(double width, double height)
    Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.Bounds.set(Xamarin.Forms.Rectangle value)
    Xamarin.Forms.Core.dll!Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(Xamarin.Forms.VisualElement child, Xamarin.Forms.Rectangle region)
    Xamarin.Forms.Core.dll!Xamarin.Forms.Page.LayoutChildren(double x, double y, double width, double height)
    Xamarin.Forms.Core.dll!Xamarin.Forms.Page.UpdateChildrenLayout()
    Xamarin.Forms.Core.dll!Xamarin.Forms.Page.OnSizeAllocated(double width, double height)
    Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.SetSize(double width, double height)
    Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.Bounds.set(Xamarin.Forms.Rectangle value)
    Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.Platform.UpdatePageSizes()
    Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.Platform.OnRendererSizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs sizeChangedEventArgs)

    • I think the LayoutChildren method of the base is not supposed to be called because when I call I get the "Cannot call an abstract base member" compilation error.
    • Adding RowDefinitions and ColumnDefinitions to the Grid in CustomLayoutView does not make any difference

    Moreover, the exception is thrown not only if the CustomLayoutViewChild is derived from Grid as shown in the code snippet. It is thrown even if it is derived from View or Layout.

  • JohnHardmanJohnHardman GBUniversity admin

    @NavaneethaKannan said:
    @JohnHardman - The exception is not thrown from the code given. It is thrown from the Xamarin.Forms.Platform.UAP dll. So I could not identify the line of code from where it is thrown. I have attached the sample and the stacktrace with the previous post. However, I have repeated the stack trace below for your convenience.

    Many thanks for providing all of the requested information.

    @NavaneethaKannan said:

    • I think the LayoutChildren method of the base is not supposed to be called because when I call I get the "Cannot call an abstract base member" compilation error.

    Correct - I've just checked the XF source code. LayoutChildren is indeed abstract.

    One more thing that you could try. In circumstances when the Grid would have no Children, add a ContentView as a child so that the Grid is not empty.

    If that doesn't work, under normal circumstances having looked at your stack trace, I would suggest logging a bug against XF. However, as you are using 2.4 and the problem does not occur in 3.x, I suspect there is no point in doing so, as the bug has presumably been fixed in a later version and it seems unlikely that any fixes will be retrofitted to 2.4 or even 2.5.

    Three options spring to mind. The first (which is what Xamarin would almost certainly say) is to upgrade XF. The second is to have a look at the changes between 2.4 and 3.x to see if you can spot what was changed to fix this in 3.x, and from there to identify a workaround. The GitHub repository is at https://github.com/xamarin/Xamarin.Forms . The third is to re-code that particular section of code to use a Layout type other than Grid. Sorry to not be more help.

Sign In or Register to comment.