滚动listview直到xamarin.forms中的结束项时,应用程序崩溃

时间:2018-02-19 12:38:05

标签: listview xamarin xamarin.forms crash

这是我的模特

public class WorkOrder: BaseViewModel
{
    public int WorkOrderKey { get; set; }
    public int? SiteFk { get; set; }
    public int? PMTaskFK { get; set; }
    public string PMTaskID { get; set; }
    public string WProgress { get; set; }
    public string ProblemDescription { get; set; }
    public string WorkOrderID { get; set; }
    public string Action { get; set; }
    public string ExtraAction { get; set; }
    public bool JobComplete { get; set; }
    public int eWorkProgress { get; set; }
    public int eWorkOrderClass { get; set; }
    public string TaskItems { get; set; }
    public string ProviderUserKey { get; set; }
    public string WorkTradeDesc { get; set; }
    public int AssetFK { get; set; }
    public string AssetID { get; set; }
    public string AssetDesc { get; set; }
    public int AssetDepartmentFK { get; set; }
    public string AssetDepartmentID { get; set; }
    public int LocationFK { get; set; }
    public string LocationID { get; set; }
    public int ParentLocationFK { get; set; }
    public string ParentLocationID { get; set; }
    public string StartDate { get; set; }
    public string DownloadedDescription { get; set; }
    public string curUser { get; set; }
    public List<Asset> AssetList { get; set; }

    private Asset asset1;
    public Asset Asset1
    {
        get { return AssetList!=null? AssetList[0]  !=null ? AssetList[0]: null : null ; }
        set { SetProperty(ref asset1, value); }
    }

    private Asset asset2;
    public Asset Asset2
    {
        get { return AssetList != null ? AssetList[1] != null ? AssetList[1] : null : null; }
        set { SetProperty(ref asset2, value); }
    }

    private Asset asset3;
    public Asset Asset3
    {
        get { return AssetList != null ? AssetList[2] != null ? AssetList[2] : null : null; }
        set { SetProperty(ref asset3, value); }
    }

    private bool isSelected;
    public bool IsSelected
    {
        get { return isSelected; }
        set { SetProperty(ref isSelected, value); }
    }
    //public bool IsSelected { get; set; } 
}
public class Asset
{
    public int Key { get; set; }
    public string Id { get; set; }
    public string Description { get; set; }
    public string Location { get; set; }
    public int DepartmentFK { get; set; }
    public string DepartmentID { get; set; }
    public int LocationFK { get; set; }
    public string LocationID { get; set; }
    public int ParentLocationFK { get; set; }
    public string ParentLocationID { get; set; }
}

这是我的xaml页面,其中我有listview绑定上述模型列表的数据

   <ListView Grid.Row="1"   x:Name="listWorkOrder" ItemsSource="{Binding ListWorkOrder}" HasUnevenRows="True" SeparatorVisibility="None"
                    BackgroundColor="Transparent" VerticalOptions="FillAndExpand" CachingStrategy="RetainElement >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>

                            <Grid Margin="0,10,0,0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                    <RowDefinition Height="*"></RowDefinition>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <controls:CustomCheckbox2 Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Checked="{Binding IsSelected}"  x:Name="chkSelectAll"  VerticalOptions="CenterAndExpand" Margin="0"  HorizontalOptions="End"  />
                                <!--<Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Source="uncheck.png" VerticalOptions="Center"></Image>-->
                                <StackLayout Grid.Column="1" Grid.Row="0" Orientation="Horizontal">
                                    <Label Text="Work Order Id:" FontAttributes="Bold"></Label>
                                    <Label  Text="{Binding WorkOrderID}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" 
                                            LineBreakMode="WordWrap" />
                                </StackLayout>

                                <Frame Margin="10,5,10,5" Grid.Column="1" Grid.Row="1" Padding="10" CornerRadius="0" BackgroundColor="White" OutlineColor="Transparent" VerticalOptions="FillAndExpand">
                                    <Frame.HasShadow>
                                        <OnPlatform x:TypeArguments="x:Boolean">
                                            <On Platform="iOS">False</On>
                                            <On Platform="Android">True</On>
                                        </OnPlatform>
                                    </Frame.HasShadow>
                                    <Grid  RowSpacing="10">
                                        <Grid.RowDefinitions >
                                            <RowDefinition Height="Auto"></RowDefinition>
                                            <RowDefinition Height="Auto"></RowDefinition>
                                            <RowDefinition Height="Auto"></RowDefinition>
                                        </Grid.RowDefinitions>

                                        <Grid Grid.Row="0" RowSpacing="0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                <ColumnDefinition Width="*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"></RowDefinition>
                                                <RowDefinition Height="Auto"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Label Grid.Column="0"  Text="Asset ID :" FontAttributes="Bold" LineBreakMode="NoWrap"></Label>
                                            <Label Grid.Column="1"  Text="{Binding Asset1.Id}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" 
                                            LineBreakMode="WordWrap" />
                                            <Label Grid.Column="0" Grid.Row="1" Text="Description :" FontAttributes="Bold" LineBreakMode="NoWrap"></Label>
                                            <Label Grid.Column="1" Grid.Row="1" Text="{Binding Asset1.Description}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" 
                                            LineBreakMode="WordWrap" />
                                        </Grid>

                                        <Grid Grid.Row="1" RowSpacing="0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                <ColumnDefinition Width="*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"></RowDefinition>
                                                <RowDefinition Height="Auto"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Label Grid.Column="0"  Text="Asset ID :" FontAttributes="Bold" LineBreakMode="NoWrap"></Label>
                                            <Label Grid.Column="1"  Text="{Binding Asset2.Id}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" 
                                            LineBreakMode="WordWrap" />
                                            <Label Grid.Column="0" Grid.Row="1" Text="Description :" FontAttributes="Bold" LineBreakMode="NoWrap"></Label>
                                            <Label Grid.Column="1" Grid.Row="1" Text="{Binding Asset2.Description}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" 
                                            LineBreakMode="WordWrap" />
                                        </Grid>

                                        <Grid Grid.Row="2" RowSpacing="0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                <ColumnDefinition Width="*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"></RowDefinition>
                                                <RowDefinition Height="Auto"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Label Grid.Column="0"  Text="Asset ID :" FontAttributes="Bold" LineBreakMode="NoWrap"></Label>
                                            <Label Grid.Column="1"  Text="{Binding Asset3.Id}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" 
                                            LineBreakMode="WordWrap" />
                                            <Label Grid.Column="0" Grid.Row="1" Text="Description :" FontAttributes="Bold" LineBreakMode="NoWrap"></Label>
                                            <Label Grid.Column="1" Grid.Row="1" Text="{Binding Asset3.Description}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" 
                                            LineBreakMode="WordWrap" />
                                        </Grid>
                                    </Grid>
                                </Frame>

                                <StackLayout Grid.Column="1" Grid.Row="2" Spacing="0">
                                    <StackLayout Orientation="Horizontal">
                                        <Label Text="PM Task Id:" FontAttributes="Bold"></Label>
                                        <Label  Text="{Binding PMTaskID}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" 
                                            LineBreakMode="WordWrap" />
                                    </StackLayout>
                                    <Button BackgroundColor="Green" Text="VIEW PM TASK ITEMS" HorizontalOptions="Start"></Button>
                                </StackLayout>


                                <BoxView  Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"   BackgroundColor="Gray" HeightRequest="1"></BoxView>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <!--<x:Arguments>
                    <ListViewCachingStrategy>
                        <OnPlatform x:TypeArguments="ListViewCachingStrategy" iOS="RecycleElement" Android="RetainElement" WinPhone="RecycleElement" />
                    </ListViewCachingStrategy>
                </x:Arguments>-->
            </ListView>

我的数据完美绑定。但是当我滚动listview直到结束应用程序崩溃。

谁有想法?我有什么不对的。

1 个答案:

答案 0 :(得分:2)

Property Asset3可能存在异常,Asset2使用正确的空值处理或检查项目计数。

例如,如果列表AssetList中只有一个项目,那么您将获得例外OutOfRangeException

解决方案,例如:

public Asset Asset1       
 {
get { return AssetList != null ? AssetList.Count >=0 ? AssetList[0] : null : null; 
}