Silverlight 可写位图

标签 silverlight

我正在使用 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/

相关文章:

silverlight - silverlight…使用相对路径的文件 super 链接按钮

silverlight - 在 Silverlight 中更改 Datagrid 标题的背景颜色

c# - Ria 服务将复杂对象作为参数传递给查询域服务方法

silverlight - 在我的 ViewModel 中使用 DataServiceCollection 时如何实现 "Blendability"

silverlight - 是否可以在 Windows Phone 7 模拟器上安装不同的字体?

silverlight - 如何在 silverlight 中显示媒体元素中耗时?

c# - 如何从 Silverlight DataContext 对象中检索数据

c# - CPU性能控制WPF

silverlight - 在 Silverlight NumericUpDown 更改值之前触发 MVVM 中继命令

Silverlight - 获取域信息