DrawingVisual未在窗口内的WPF Canvas中显示

hel*_*ker 3 wpf drawingvisual

我创建了一个最小的项目,以便在WPF中使用DrawingVisuals进行"开始"(到目前为止非常初学者).

我的项目仅包含主窗口的XAML和代码.这个项目的唯一目的是打开一个窗口,显示一些填充可用空间的"信号噪音".

MainWindow.xaml是这样的:

<Window x:Class="MinimalPlotter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MinimalPlotter"
        Title="MainWindow" Width="1200" Height="800"
        WindowStartupLocation="CenterScreen">

        <Canvas Height="500" Width="700" x:Name="drawingArea" Margin="50" Background="Gray">
            <local:VisualHost Canvas.Top="0" Canvas.Left="0" x:Name="host" Width="700" Height="500" />
        </Canvas>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码背后是这样的:

namespace MinimalPlotter
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class VisualHost : FrameworkElement
    {

        public VisualHost()
        {
            int h = (int)this.Height;
            int w = (int)this.Width;

            Random random = new Random();
            double r;

            DrawingVisual path = new DrawingVisual();

            StreamGeometry g = new StreamGeometry();
            StreamGeometryContext cr = g.Open();
            cr.BeginFigure(new Point(0,0), false, false);
            for (int i = 0; i < w; i++)
            {
                // ugly calculations below to get the signal centered in container
                r = (random.NextDouble()-0.5) * h -h/2;
                cr.LineTo(new Point(i, r), true, false);
            }
            cr.Close();
            g.Freeze();

            DrawingContext crx = path.RenderOpen();
            Pen p = new Pen(Brushes.Black, 2);
            crx.DrawGeometry(null, p, g);

            // Ellipse included for "visual debugging"
            crx.DrawEllipse(Brushes.Red, p, new Point(50,50), 45, 20);

            crx.Close();

            this.AddVisualChild(path);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是:当窗口打开时,Canvas按预期显示在中心(灰色背景),但没有信号被绘制.此代码的先前版本使用路径几何图形工作正常,但使用DrawingVisual时未显示几何图形(甚至不包括用于调试的椭圆几何图形).

谢谢阅读!

Cle*_*ens 6

您的VisualHost类还必须重写VisualChildrenCount属性和GetVisualChild方法:

public class VisualHost : FrameworkElement
{
    private DrawingVisual path = new DrawingVisual();

    public VisualHost()
    {
        ...
        AddVisualChild(path);
    }

    protected override int VisualChildrenCount
    {
        get { return 1; }
    }

    protected override Visual GetVisualChild(int index)
    {
        return path;
    }
}
Run Code Online (Sandbox Code Playgroud)

另请注意,在using语句中使用StreamGeometryContext和DrawingContext等IDisposable对象被认为是一种好习惯:

var g = new StreamGeometry();
using (var cr = g.Open())
{
    cr.BeginFigure(new Point(0,0), false, false);
    ...
    // no need for cr.Close()
}

using (var crx = path.RenderOpen())
{
    var p = new Pen(Brushes.Black, 2);
    crx.DrawGeometry(null, p, g);
    crx.DrawEllipse(Brushes.Red, p, new Point(50,50), 45, 20);
    // no need for crx.Close()
}
Run Code Online (Sandbox Code Playgroud)