gpt4 book ai didi

wpf - DrawingVisual 不显示在窗口内的 WPF Canvas 中

转载 作者:行者123 更新时间:2023-12-04 13:38:38 24 4
gpt4 key购买 nike

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

问题是:当窗口打开时,Canvas 显示在中心,如预期的那样(灰色背景),但没有绘制信号。此代码的先前版本使用 Path 几何图形运行良好,但使用 DrawingVisual 时不显示任何几何图形(甚至不包括用于调试的椭圆几何图形)。

感谢阅读!

最佳答案

您的 VisualHost 类还必须覆盖 VisualChildrenCount属性(property)和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;
}
}

另请注意,在 using 语句中使用 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()
}

关于wpf - DrawingVisual 不显示在窗口内的 WPF Canvas 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14528836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com