Silverlight可写位图
我正在使用Silverlight WriteableBitmap命令使用以下代码呈现“饼图”Silverlight可写位图,silverlight,Silverlight,我正在使用Silverlight WriteableBitmap命令使用以下代码呈现“饼图” Chart GetChart() { Chart newChart = new Chart() { Width = 100.0, Height = 100.0 }; PieSeries pieSeries = new PieSeries(); pieSeri
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);
Chart GetChart()
{
Chart newChart=new Chart(){Width=100.0,
高度=100.0};
PieSeries PieSeries=新PieSeries();
PieSSeries.SetBinding(PieSSeries.ItemsSourceProperty,新绑定());
pieSeries.DependentValueBinding=新绑定(“值”);
pieSeries.IndependentValueBinding=新绑定(“键”);
pieSeries.AnimationSequence=AnimationSequence.FirstToLast;
pieSeries.IsSelectionEnabled=true;
newChart.Series.Add(pieSeries);
newChart.SetValue(Chart.DataContextProperty,新的KeyValuePair[]
{
新的KeyValuePair(“工作”,9),
新的KeyValuePair(“驱动”,2),
新的KeyValuePair(“系列”,4),
新的KeyValuePair(“睡眠”,8),
新的KeyValuePair(“朋友”,1)
});
返回新图表;
}
WriteableBitmap bmapPreviewCanvas=新的WriteableBitmap(GetChart,null);
我所期望的结果是一个带有PieChart的位图。我得到的是一个没有任何图形的背景位图
问题是:我应该怎么做才能在变量“bmapPreviewCanvas”中呈现饼图
编辑:这与ANIMATIONSEQUENCE有关吗?我认为您的问题在于您试图创建位图的图表控件实例不在可视化树上 实际上我自己并没有尝试过,但我相信如果UIElement不是可视化树的一部分,您必须显式地使用WritableBitmap类的。在调用Render()之前,还必须对元素调用InvalidateMeasure(),然后对可写位图调用Invalidate(),才能实际渲染位图 从MSDN文档中: 调用此方法后,必须调用Invalidate才能渲染位图 此方法不支持不属于可视树的UIElement对象。在调用Render之前,需要为不在可视化树中的UIElement对象调用Measure和Arrange 我会尝试以下方法:
var chart = GetChart();
chart.InvalidateMeasure();
WriteableBitmap bmapPreviewCanvas = new WriteableBitmap(chart.Width, chart.Height);
bmpaPreviewCanvas.Render(chart, null);
bmapPreviewCanvas.Invalidate();
丹,你又对了。如果将位图添加到画布或可视化树中,我会看到生成的位图。非常感谢。我把这个问题再向前推进一步。我正在使用ImageBrush中的渲染图像,并尝试用它填充矩形。知道为什么矩形不会显示图像吗?我不知道没有看到更多的代码,但我确实找到了一个关于可能的可写位图/图像画笔错误的帖子:Dan,谢谢你的链接。我在你发布的同一时间也遇到了同样的链接。似乎这是SL3中的一个已知问题。谢谢你的帮助。