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

时间:2013-09-23 13:29:10

标签: c# charts windows-runtime winrt-xaml winrt-xaml-toolkit

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

异常详情

  

Windows.UI.Xaml.UIElement.Measure的System.Exception(大小   availableSize)at   WinRTXamlToolkit.Controls.DataVisualization.Charting.Primitives.EdgePanel.MeasureOverride(大小   在Windows.UI.Xaml.FrameworkElement.MeasureOverride(大小   availableSize)

     

“错误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; }
}

1 个答案:

答案 0 :(得分:1)

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

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#

首先在页面中添加系列(“图表:Stacked100BarSeries”)。在为图表控件添加系列之前,清除系列。

例如:

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