我在我的应用程序中使用了 winrt xaml 工具包的图表。在某些屏幕中图表工作正常,但在某些屏幕中它现在抛出 UnhandledException(之前它工作正常。我没有更新 ANYHTING)。我没有得到太多的异常细节。图表有什么问题?
异常详情
System.Exception 在 Windows.UI.Xaml.UIElement.Measure(Size availableSize) 在 WinRTXamlToolkit.Controls.DataVisualization.Charting.Primitives.EdgePanel.MeasureOverride(大小约束) 在 Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)
“对 COM 组件的调用已返回错误 HRESULT E_FAIL。”
这是重新生成它的代码。我在后面的代码中做所有的事情,因为在真正的应用程序中会有动态数量的图表。
XAML
<Page.Resources>
<Style TargetType="charting:ColumnDataPoint" x:Key="MyColumnDataPointStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="charting:ColumnDataPoint">
<Grid>
<ToolTipService.ToolTip>
<ContentControl>
<TextBlock Text="{Binding Tooltip}" TextAlignment="Center" />
</ContentControl>
</ToolTipService.ToolTip>
<Rectangle Fill="{Binding Color}" Stroke="{Binding Color}" StrokeThickness="3" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<charting:Chart x:Name="ChartSalesRevenue" Margin="70,0" />
</Grid>
C#
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ChartSalesRevenue.Series.Clear();
ChartSalesRevenue.Series.Add(new StackedColumnSeries());
Binding DependentBinding = new Binding();
Binding IndependentBinding = new Binding();
IndependentBinding.Converter = new DateToStringConverter();
SeriesDefinition series;
series = new SeriesDefinition();
series.DataPointStyle = this.Resources["MyColumnDataPointStyle"] as Style;
DependentBinding.Path = new PropertyPath("Units");
IndependentBinding.Path = new PropertyPath("Begin_Date");
series.DependentValueBinding = DependentBinding;
series.IndependentValueBinding = IndependentBinding;
series.ItemsSource = GetDataItems();
((StackedColumnSeries)ChartSalesRevenue.Series[0]).SeriesDefinitions.Add(series);
}
private List<DataItem> GetDataItems()
{
return new List<DataItem>
{
new DataItem("Tooltip 1", new SolidColorBrush(Colors.Red), 5, DateTime.Now.AddDays(-1)),
new DataItem("Tooltip 2", new SolidColorBrush(Colors.Violet), 3, DateTime.Now.AddDays(1)),
new DataItem("Tooltip 3", new SolidColorBrush(Colors.PaleGoldenrod), 22, DateTime.Now.AddDays(-2)),
new DataItem("Tooltip 4", new SolidColorBrush(Colors.DarkBlue), 15, DateTime.Now.AddDays(2)),
new DataItem("Tooltip 5", new SolidColorBrush(Colors.DeepPink), 9, DateTime.Now),
};
}
public class DateToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string Language)
{
return ((DateTime)value).ToString("MMM dd, yyyy", System.Globalization.CultureInfo.InvariantCulture);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
public class DataItem
{
public DataItem(string _Tooltip, SolidColorBrush _Color, int _Units, DateTime _Begin_Date)
{
Tooltip = _Tooltip;
Color = _Color;
Units = _Units;
Begin_Date = _Begin_Date;
}
public string Tooltip { get; set; }
public SolidColorBrush Color { get; set; }
public int Units { get; set; }
public DateTime Begin_Date { get; set; }
}