我在我的應用程序中使用了winrt xaml工具箱圖表。在某些屏幕圖表工作正常,但在某些屏幕上它現在拋出UnhandledException(早期它工作正常,我沒有更新ANYHTING)。我沒有得到太多的例外細節。圖表有什麼問題?從winrt xaml工具包使用圖表時出現UnhandledException
異常詳細
System.Exception的在Windows.UI.Xaml.UIElement.Measure(尺碼 availableSize)在 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; }
}
我跑同樣的問題一次,一定要指定一個最小寬度和最低高度屬性爲圖表組件或其父。請查看此[幫助鏈接](http://winrtxamltoolkit.codeplex.com/discussions/429746)以獲取其他解決方案。我已經通過這兩種方式在運行時創建動態圖表。 –