我正在使用 Silverlight WriteableBitmap 命令通过以下代码呈现“饼图”。
Chart GetChart()
{
Chart newChart = new Chart() { Width = 100.0,
Height = 100.0 };
PieSeries pieSeries = new PieSeries();
pieSeries.SetBinding(PieSeries.ItemsSourceProperty, new Binding());
pieSeries.DependentValueBinding = new Binding("Value");
pieSeries.IndependentValueBinding = new Binding("Key");
pieSeries.AnimationSequence = AnimationSequence.FirstToLast;
pieSeries.IsSelectionEnabled = true;
newChart.Series.Add(pieSeries);
newChart.SetValue(Chart.DataContextProperty, new KeyValuePair<string, int>[]
{
new KeyValuePair<string, int>("Work", 9),
new KeyValuePair<string, int>("Driving", 2),
new KeyValuePair<string, int>("Family", 4),
new KeyValuePair<string, int>("Sleep", 8),
new KeyValuePair<string, int>("Friends", 1)
});
return newChart;
}
WriteableBitmap bmapPreviewCanvas = new WriteableBitmap(GetChart, null);
我期待的结果是带有饼图的位图。我得到的是一个带有背景的位图,没有任何饼图。
问题是:我应该怎么做才能在变量“bmapPreviewCanvas”中呈现饼图?
编辑:这与 ANIMATIONSEQUENCE 有关系吗?
最佳答案
我相信您的问题是您尝试创建位图的图表控件实例不在可视化树上。
我自己实际上并没有尝试过这个,但我相信您必须明确使用 Render() method如果 UIElement 不是可视化树的一部分,则为 WritableBitmap 类。您还必须在调用 Render() 之前对元素调用 InvalidateMeasure(),然后在 WritableBitmap 上调用 Invalidate() 才能实际渲染位图。
来自 MSDN 文档:
After calling this method, you must call Invalidate in order to render the bitmap.
This method does support UIElement objects that are not part of the visual tree. You are required to call Measure and Arrange for UIElement objects that are not in the visual tree, before calling Render.
我会尝试以下方法:
var chart = GetChart();
chart.InvalidateMeasure();
WriteableBitmap bmapPreviewCanvas = new WriteableBitmap(chart.Width, chart.Height);
bmpaPreviewCanvas.Render(chart, null);
bmapPreviewCanvas.Invalidate();
关于Silverlight 可写位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2330040/