最近,我们在我的 Xamarin.Forms 应用程序中发现了一个错误,该错误似乎与 Syncfusion 元素“SfChart”相关。
我们有一个通过数据绑定获取数据的 4 轴图表。当一个承载数据的对象被图表用来绘制线条时,会发生错误,该对象被删除、添加或更改。
此数据存储在 ObservableCollection 中,并在更改时发出 PropertyChanged 信号。
每当将不同的数据对象分配给数据列表的索引,绑定到各个 FastLineSeries 的 ItemSources 时,我有时会收到以下错误:
[mono-rt] [错误] 致命的未处理异常:System.ArgumentOutOfRangeException:索引必须在列表的范围内。
[mono-rt] 参数名称:index
[mono-rt] 在 System.Collections.Generic.List`1[T].Insert (System.Int32 index, T item) [0x00009] in /Users/builder/jenkins/workspace/archive-mono/2020-02/ android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:688
[mono-rt] 在 Com.Syncfusion.Charts.ChartSeries.SetIndividualPoint(System.Int32 索引,System.Object 值,System.Boolean 替换)[0x007d4] 在 <5223039dbed24acb9c9c1764d59d17de>:0
[mono-rt] 在 Com.Syncfusion.Charts.ChartSeries.OnDataSource_CollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00041] 在 <5223039dbed24acb9c9c1764d59d17de>:0
[mono-rt] at (wrapper delegate-invoke) .invoke_void_object_NotifyCollectionChangedEventArgs(object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
[mono-rt] 在 System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00018] 在 /Users/builder/jenkins/workspace/archive-mono/2020-02 /android/release/external/corefx/src/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs:263
[mono-rt] 在 MvvmCross.ViewModels.MvxObservableCollection`1[T].<>n__0 (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) <0x727c3f6d68 + 0x00043> in <687f2e5f7efd4e33b498a18ba9db221f>:0
[mono-rt] 在 MvvmCross.ViewModels.MvxObservableCollection`1+<>c_DisplayClass7_0[T].b0 () [0x00000] in //MvvmCross/ViewModels/MvxObservableCollection.cs:72
[mono-rt] 在 MvvmCross.Base.MvxMainThreadAsyncDispatcher+<>c_DisplayClass0_0.b0 () [0x00000] 在 //MvvmCross/Base/MvxMainThreadAsyncDispatcher.cs:16
[mono-rt] 在 MvvmCross.Base.MvxMainThreadAsyncDispatcher+<>c_DisplayClass1_0.b0 () [0x00011] 在 //MvvmCross/Base/MvxMainThreadAsyncDispatcher.cs:27
[mono-rt] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.26(intptr,intptr)
[mono-rt] at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.26(intptr,intptr)
似乎有时在更改或删除 LineSeries 的数据后,旧数据保留在 UI 元素中,并且绘制了两个具有相同颜色的相同 LineSeries,即使该数据不在数据绑定列表中的任何位置。
奇怪的是,图表绘制了两条颜色相同但值差异很大的线,尽管一个 LineSeries 应该能够从中获取数据的只有一种可能的绑定。
最好的问候, OB
XAML 如下所示:
<Grid
Grid.Row="0"
Grid.Column="0"
BackgroundColor="#0072BC"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapChartSelectionPopup}" CommandParameter="0" />
</Grid.GestureRecognizers>
<Label
Grid.Column="0"
BackgroundColor="#ED1B2F"
HorizontalOptions="FillAndExpand" />
<Label
Grid.Column="1"
Grid.ColumnSpan="4"
HorizontalOptions="Start"
Text="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[0].Name}" />
<Label
x:Name="UnitLabel1"
Grid.Column="5"
Grid.ColumnSpan="2"
HorizontalOptions="FillAndExpand"
Text="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[0].Unit}"
TextColor="White"
VerticalOptions="FillAndExpand" />
<Image
Grid.Column="6"
Grid.ColumnSpan="2"
Margin="0,0,-5,0"
HorizontalOptions="Center"
Scale="0.7"
Source="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[0].ImagePath}" />
</Grid>
<Grid
Grid.Row="0"
Grid.Column="1"
BackgroundColor="#0072BC"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapChartSelectionPopup}" CommandParameter="1" />
</Grid.GestureRecognizers>
<Label
Grid.Column="0"
BackgroundColor="#8DC63F"
HorizontalOptions="FillAndExpand" />
<Label
Grid.Column="1"
Grid.ColumnSpan="4"
HorizontalOptions="Start"
Text="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[1].Name}" />
<Label
x:Name="UnitLabel2"
Grid.Column="5"
Grid.ColumnSpan="2"
HorizontalOptions="FillAndExpand"
Text="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[1].Unit}"
TextColor="White"
VerticalOptions="FillAndExpand" />
<Image
Grid.Column="6"
Grid.ColumnSpan="2"
Margin="0,0,-5,0"
HorizontalOptions="Center"
Scale="0.7"
Source="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[1].ImagePath}" />
</Grid>
<Grid
Grid.Row="1"
Grid.Column="0"
BackgroundColor="#0072BC"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapChartSelectionPopup}" CommandParameter="2" />
</Grid.GestureRecognizers>
<Label
Grid.Column="0"
BackgroundColor="#07426E"
HorizontalOptions="FillAndExpand" />
<Label
Grid.Column="1"
Grid.ColumnSpan="4"
HorizontalOptions="Start"
Text="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[2].Name}" />
<Label
x:Name="UnitLabel3"
Grid.Column="5"
Grid.ColumnSpan="2"
HorizontalOptions="FillAndExpand"
Text="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[2].Unit}"
TextColor="White"
VerticalOptions="FillAndExpand" />
<Image
Grid.Column="6"
Grid.ColumnSpan="2"
Margin="0,0,-5,0"
HorizontalOptions="Center"
Scale="0.7"
Source="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[2].ImagePath}" />
</Grid>
<Grid
Grid.Row="1"
Grid.Column="1"
BackgroundColor="#0072BC"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapChartSelectionPopup}" CommandParameter="3" />
</Grid.GestureRecognizers>
<Label
Grid.Column="0"
BackgroundColor="#FFCC00"
HorizontalOptions="FillAndExpand" />
<Label
Grid.Column="1"
Grid.ColumnSpan="4"
HorizontalOptions="Start"
Text="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[3].Name}" />
<Label
x:Name="UnitLabel4"
Grid.Column="5"
Grid.ColumnSpan="2"
HorizontalOptions="FillAndExpand"
Text="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[3].Unit}"
TextColor="White"
VerticalOptions="FillAndExpand" />
<Image
Grid.Column="6"
Grid.ColumnSpan="2"
Margin="0,0,-5,0"
HorizontalOptions="Center"
Scale="0.7"
Source="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[3].ImagePath}" />
</Grid>
</Grid>
</StackLayout>
<StackLayout
HorizontalOptions="FillAndExpand"
Orientation="Horizontal"
RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0.75}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=0.9}"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=0.05}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0.25}"
VerticalOptions="FillAndExpand">
<sfChart:SfChart
x:Name="chart"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<sfChart:SfChart.PrimaryAxis>
<sfChart:DateTimeAxis />
</sfChart:SfChart.PrimaryAxis>
<sfChart:SfChart.ChartBehaviors>
<sfChart:ChartZoomPanBehavior EnableDirectionalZooming="True" EnableDoubleTap="True" />
</sfChart:SfChart.ChartBehaviors>
<sfChart:SfChart.Series>
<sfChart:FastLineSeries
x:Name="flSeries1"
ItemsSource="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[0].ChartDataLog}"
XBindingPath="TestDT"
YBindingPath="Value"
Color="#ED1B2F" />
<sfChart:FastLineSeries
x:Name="flSeries2"
ItemsSource="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[1].ChartDataLog}"
XBindingPath="TestDT"
YBindingPath="Value"
Color="#8DC63F" />
<sfChart:FastLineSeries
x:Name="flSeries3"
ItemsSource="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[2].ChartDataLog}"
XBindingPath="TestDT"
YBindingPath="Value"
Color="#07426E" />
<sfChart:FastLineSeries
x:Name="flSeries4"
ItemsSource="{Binding PanelAppModel.PanelApp.ChartModel.ChartDataList[3].ChartDataLog}"
XBindingPath="TestDT"
YBindingPath="Value"
Color="#FFCC00" />
</sfChart:SfChart.Series>
</sfChart:SfChart>
</StackLayout>