XAML课程:
<?xml version="1.0" encoding="utf-8" ?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XYZ.UI.CustomControls;assembly=XYZ.UI"xmlns:controls="clr- namespace:Xamarin.Forms.Labs.Controls;assembly=Xamarin.Forms.Labs"
x:Class="XYZ.UI.Views.Layouts.GHeaderLayout">
<Grid ColumnSpacing="0" BackgroundColor="White" RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<StackLayout BackgroundColor="White" Grid.Row="0" Grid.Column="0" Orientation="Vertical" Padding="10,0,10,0" >
<Label Text="{Binding CName}" TextColor="Black" HorizontalOptions="StartAndExpand" VerticalOptions="Start" Font="14" />
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" Spacing="0">
<Label Text="{Binding JName}" TextColor="Black" HorizontalOptions="Start" VerticalOptions="Start" Font="14"></Label>
<Image Source="drp_btn.png" WidthRequest="50" HeightRequest="15" ></Image>
</StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding OSCommand}"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<StackLayout BackgroundColor="White" Grid.Row="0" Grid.Column="1" VerticalOptions="End" Spacing="1">
<Label x:Name="UserTotalHeader" Text="Total" TextColor="Black" HorizontalOptions="CenterAndExpand" VerticalOptions="End" Font="14"></Label>
</StackLayout>
<StackLayout x:Name="GEPHeader" Grid.Row="0" BackgroundColor="White" Grid.Column="2" Orientation="Vertical" HorizontalOptions="FillAndExpand" Spacing="0">
<Grid RowSpacing="0" BackgroundColor="White">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackLayout StyleId="A" x:Name="PNameLayout" Grid.Row="0" Grid.Column="0" >
<ScrollView Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<local:ItemsSourceBehavior StyleId="C" ItemsSource="{Binding PName}" Orientation="Horizontal" Scroll="Horizontal" >
<local:ItemsSourceBehavior.ItemTemplate>
<DataTemplate >
<StackLayout StyleId="D" Orientation="Vertical" HorizontalOptions="FillAndExpand" Spacing="0" >
<Label Text="{Binding UD}" LineBreakMode="WordWrap" XAlign="Center" Font="14" TextColor="Gray" HeightRequest="40" FontFamily="Bold" VerticalOptions="CenterAndExpand" />
<Label Text="{Binding UnitCode}" XAlign="Center" Font="14" TextColor="Gray" HeightRequest="40" FontFamily="Bold" VerticalOptions="CenterAndExpand"/>
</StackLayout>
</DataTemplate>
</local:ItemsSourceBehavior.ItemTemplate>
</local:ItemsSourceBehavior>
</ScrollView>
</StackLayout>
</Grid>
</StackLayout>
ItemsSourceBehavior:它在IOS中提供NullReference异常(在Android中正常工作)LayoutChildren方法,它继承自Layout。我觉得崩溃发生在调用LayoutChildren方法之前。
namespace XYZ.UI.CustomControls{
public class ItemsSourceBehavior : Layout<View>
{
protected override void LayoutChildren(double x, double y, double width, double height)
{
try {
if (layoutWidth == 0)
{
layoutWidth = width;
}
double rowHeight = 0;
double yPos = y, xPos = x;
foreach (var child in Children.Where(c => c.IsVisible))
{
//var request = child.GetSizeRequest(width, height);
//double childWidth = request.Request.Width;
//double childHeight = request.Request.Height;
//rowHeight = Math.Max(rowHeight, childHeight);
//if (xPos + childWidth > width)
//{
// xPos = x;
// yPos += rowHeight + Spacing;
// rowHeight = 0;
if (Children.Count < 4)
{
var region = new Rectangle(xPos, yPos, layoutWidth / Children.Count, 80);
LayoutChildIntoBoundingRegion(child, region);
xPos += region.Width + Spacing;
}
else
{
var region = new Rectangle(xPos, yPos, layoutWidth / 4, 80);
LayoutChildIntoBoundingRegion(child, region);
xPos += region.Width + Spacing;
}
}
}catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception:ex{0}", ex);
}}}
输出窗口:
已解决的待处理断点位于&#39; ItemsSourceBehavior.cs:362,1&#39;取消XYZ.UI.CustomControls.ItemsSourceBehavior.LayoutChildren(双x,双y,双宽,双高)[0x00000]。 线程完成:#6 线程&#39;未知&#39; (0x6)已退出代码0(0x0)。 未处理的例外:
System.NullReferenceException:未将对象引用设置为对象的实例
线程完成:#7 线程&#39;未知&#39; (0x7)已退出代码0(0x0)。 2016-08-09 19:08:32.542 XYZ [4960:26908] 未处理的异常: System.NullReferenceException:未将对象引用设置为对象的实例 在Xamarin.Forms.StackLayout.LayoutChildren(双x,双y,双宽,双高)[0x00092] in:0 在Xamarin.Forms.Layout.UpdateChildrenLayout()[0x000c7] in:0 在Xamarin.Forms.Layout.OnSizeAllocated(双倍宽度,双倍高度)[0x0000f] in:0
=============================================== ================== 执行本机代码时获得SIGABRT。这通常表明 单声道运行时或其中一个本机库中的致命错误
发射成功。该应用程序&#39; XYZ&#39;已在iPad 2 iOS 8.4&#39;上成功推出。 该应用已被终止。