UnhandledException 同时使用 winrt xaml 工具包中的图表

本文关键字:工具包 xaml winrt UnhandledException | 更新日期: 2023-09-27 18:32:37

我正在我的应用程序中使用 winrt xaml 工具包的图表。在某些屏幕中,图表工作正常,但在某些屏幕中,它现在抛出UnhandledException(早些时候它工作正常。我没有更新 ANYHTING)。我没有收到太多异常细节。图表有什么问题?

异常详细信息

System.Exception at Windows.UI.Xaml.UIElement.Measure(Size 可用大小)在 WinRTXamlToolkit.Controls.DataVisualization.Charting.Primitives.EdgePanel.MeasureOverride(Size constraint) at Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size 可用大小)

"错误 HRESULT E_FAIL已从对 COM 的调用返回 组件。

下面是重新生成它的代码。我正在后面的代码中做所有的事情,因为在实际应用程序中会有动态数量的图表。

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

UnhandledException 同时使用 winrt xaml 工具包中的图表

我遇到了同样的问题,并找到了一个非常简单的解决方案。

XAML

    <charting:Chart x:Name="ChartSalesRevenue" Margin="70,0" >
        <charting:Stacked100BarSeries>
            <charting:SeriesDefinition
                    DependentValuePath="Value"
                    IndependentValuePath="Name"
                    IsTapEnabled="True"
                    Title="" />
        </charting:Stacked100BarSeries>
    </charting:Chart>

C#

在页面中,首先添加系列("图表:堆叠100条形系列")。在为图表控件添加系列之前,请清除序列。

例如:

                ChartSalesRevenue.Series.Clear();
                ChartSalesRevenue.Series.Add(...);