winrt xaml toolkit charts动态添加图表报Error HRESULT E_FAIL has been returned from a call to a COM componen

首先需引用 控件

WinRTXamlToolkit、WinRTXamlToolkit.Controls.DataVisualization

前台XAML:

xmlns:charting="using:WinRTXamlToolkit.Controls.DataVisualization.Charting"
 <charting:Chart
                x:Name="Stacked100Area"
                Title=""
                Margin="0,0">
 </charting:Chart>

后台:

            //Stacked100AreaSeries Stacked100LineSeries StackedAreaSeries StackedLineSeries 父容器初始化底下需要内容(可以在刚开始给它内容(Series),然后使用时清除掉Series.Clear()),否则会报错
            //首先定义stacked100Area
            Stacked100AreaSeries stacked100Area = new Stacked100AreaSeries();

            //给stacked100Area的SeriesDefinitions赋值,在for循环下赋值多组数据
            SeriesDefinition s = new SeriesDefinition();
            s.Title = chartListDic[i].First().Description;
            s.ItemsSource = chartListDic[i];
            s.IndependentValueBinding = new Binding { Path = new PropertyPath("Name") };
            s.DependentValueBinding = new Binding { Path = new PropertyPath("Value") };
            s.IsTapEnabled = true;
            stacked100Area.SeriesDefinitions.Add(s);

            //将stacked100Area添加到Chart Stacked100Area
            Stacked100Area.Series.Clear();
            hideAllFilpButThis(Stacked100AreaFlip);
            Stacked100Area.Title = chartListDic[0].First().Title;
            Stacked100Area.Series.Add(stacked100Area);

会报错如下:

System.Exception at Windows.UI.Xaml.UIElement.Measure(Size availableSize) at WinRTXamlToolkit.Controls.DataVisualization.Charting.Primitives.EdgePanel.MeasureOverride(Size constraint) at Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)

"Error HRESULT E_FAIL has been returned from a call to a COM component."

解决方案:

Stacked100AreaSeries Stacked100LineSeries StackedAreaSeries StackedLineSeries 父容器初始化底下需要内容(可以在刚开始给它内容(Series),然后使用时清除掉Series.Clear()),否则会报错

在xmal页面上Stacked100Area初始化添加charting:Stacked100AreaSeries,如下

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

然后添加时首先清除掉charting:Stacked100AreaSeries,然后动态加图表Stacked100AreaSeries

                Stacked100Area.Series.Clear();//添加时首先清除掉charting:Stacked100AreaSeries
                Stacked100Area.Series.Add(...);//然后动态加图表Stacked100AreaSeries

大家可以参考:

UnhandledException while using chart from winrt xaml toolkit



你可能感兴趣的:(error,HRESULT,E_FAIL,Charting,MeasureOverride)