将包含网格的Layout <view>添加到StackLayout

时间:2018-10-01 09:30:15

标签: xamarin.forms xamarin.uwp

在Xamarin.Forms UWP中,以下代码在Xamarin.Forms 2.4.0.280版本中引发System.NullReferenceException。

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

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


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

下面是该异常的堆栈跟踪。

  

Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.VisualElementRenderer.GetDesiredSize(double widthConstraint,double heightConstraint)       Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.Platform.Xamarin.Forms.Internals.IPlatform.GetNativeSize(Xamarin.Forms.VisualElement元素,double widthConstraint,double heightConstraint)
      Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.OnSizeRequest(double widthConstraint,double heightConstraint)
      Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.OnMeasure(双倍宽度约束,双倍高度约束)
      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标志)
      Xamarin.Forms.Core.dll!Xamarin.Forms.StackLayout.CalculateNaiveLayout(Xamarin.Forms.StackLayout.LayoutInformation布局,Xamarin.Forms.StackOrientation方向,double x,double y,double widthConstraint,double heightConstraint)
      Xamarin.Forms.Core.dll!Xamarin.Forms.StackLayout.CalculateLayout(Xamarin.Forms.StackLayout.LayoutInformation布局,双x,双y,双widthConstraint,双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(两倍宽度,两倍高度)
      Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.SetSize(双倍宽度,双倍高度)       Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.Bounds.set(Xamarin.Forms.Rectangle值)
      Xamarin.Forms.Core.dll!Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(Xamarin.Forms.VisualElement子级,Xamarin.Forms.Rectangle区域)
      Xamarin.Forms.Core.dll!Xamarin.Forms.Page.LayoutChildren(双倍x,双倍y,双倍宽度,双倍高度)
      Xamarin.Forms.Core.dll!Xamarin.Forms.Page.UpdateChildrenLayout()
      Xamarin.Forms.Core.dll!Xamarin.Forms.Page.OnSizeAllocated(双倍宽度,双倍高度)
      Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.SetSize(双倍宽度,双倍高度)       Xamarin.Forms.Core.dll!Xamarin.Forms.VisualElement.Bounds.set(Xamarin.Forms.Rectangle值)
      Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.Platform.UpdatePageSizes()
      Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.Platform.OnRendererSizeChanged(对象发送者,Windows.UI.Xaml.SizeChangedEventArgs sizeChangedEventArgs)

高于3.0的Xamarin.Forms版本不会引发异常。但我想继续使用2.4.0.280。是否有解决方法,可以在不升级Xamarin.Forms的情况下避免此异常?

1 个答案:

答案 0 :(得分:0)

该问题似乎已在新版本上修复。但是,如果您不想更新到固定的更高版本,则可以尝试将StackLayout更改为其他Layouts,例如Grid