Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
DrawingVisual未显示在窗口内的WPF画布中_Wpf_Drawingvisual - Fatal编程技术网

DrawingVisual未显示在窗口内的WPF画布中

DrawingVisual未显示在窗口内的WPF画布中,wpf,drawingvisual,Wpf,Drawingvisual,我创建了一个最小的项目,以便“开始”使用WPF中的DrawingVisuals进行绘图(到目前为止,我还是非常初学者) 我的项目只包含主窗口的XAML和代码隐藏。该项目的唯一目的是打开一扇窗,在可用空间中显示一些“信号噪声” MainWindow.xaml是这样的: <Window x:Class="MinimalPlotter.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentat

我创建了一个最小的项目,以便“开始”使用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>

其背后的代码是:

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);
        }
    }
}
namespace绘图仪
{
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
公共类VisualHost:FrameworkElement
{
公共可视主机()
{
int h=(int)this.Height;
int w=(int)this.Width;
随机=新随机();
双r;
DrawingVisual路径=新建DrawingVisual();
StreamGeometry g=新的StreamGeometry();
StreamGeometryContext cr=g.Open();
cr.beginigure(新点(0,0),假,假);
对于(int i=0;i
问题是:当窗口打开时,画布如预期的那样显示在中心(带有灰色背景),但没有绘制信号。此代码的早期版本使用路径几何体运行良好,但在DrawingVisual中未显示任何几何体(甚至不包括用于调试的椭圆几何体)


谢谢你的阅读

VisualHost类还必须重写属性和方法:


还请注意,使用
语句在
中使用IDisposable对象(如StreamGeometryContext和DrawingContext)被认为是良好的做法:

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()
}

成功了。我必须弄清楚为什么会这样,但我记得一些教程提到了这些覆盖。谢谢所有细节都在MSDN的文章中。
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()
}