【问题标题】:app crash when scrolling listview till end items in xamarin.forms滚动列表视图直到 xamarin.forms 中的最终项目时应用程序崩溃
【发布时间】:2018-02-19 20:59:58
【问题描述】:

这是我的模特

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 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>

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

有人知道吗?是不是我做错了什么。

【问题讨论】:

  • 您是否尝试在 CachingStrategy 上使用 RecycleElement 而不是 RetainElement?
  • 是的,我试过了。没有运气
  • Xaml 看起来不错,应用是否在两个设备(Android 和 iOS)上都崩溃了?
  • 我现在只是在检查安卓。它在 android 中崩溃
  • 你检查输出信息了吗?

标签: listview xamarin xamarin.forms crash


【解决方案1】:

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

例如,如果您在列表中只有一项 AssetList,那么您将收到异常 OutOfRangeException

解决方案例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多