简体   繁体   English

如何以编程方式将简单形状绘制到WPF xaml定义的画布上

[英]How to draw simple shapes onto a WPF xaml-defined canvas programatically

I am porting (or attempting to) legacy code from a very slow WinForms app to WPF. 我正在从一个非常慢的WinForms应用程序移植(或尝试)遗留代码到WPF。 The WinForms app falls down at the rendering of large images, and it has to rerender very frequently as the user pans around so it has to go. WinForms应用程序在渲染大图像时会失败,并且当用户平移时必须非常频繁地重新渲染,因此必须这样做。

I have a system in place to draw a canvas in xaml that allows zooming and panning with a custom child of Border, in the xaml it is named imaginatively as "canvas". 我有一个系统来在xaml中绘制画布,允许使用Border的自定义子项进行缩放和平移,在xaml中它被富有想象地命名为“canvas”。 I am having issues drawing simple ellipses to the canvas from other classes. 我有问题从其他类向画布绘制简单的省略号。

namespace ZoomPan
{   
    public class DisplayManager
    {
        protected void DrawIcon(Locale locale, Color color) 
        {
            Brush sensorBrush = new SolidColorBrush(color);
            Brush sensorOutline = new SolidColorBrush(Colors.Black);
            int x = locale.x;
            int y = locale.y;
            halfSize = 10;
            DrawEllipse(locale.Name, sensorBrush, sensorOutline, x - halfSize, y - halfSize, 2 * halfSize, 2 * halfSize);
        }   

        public void DrawEllipse(string name, Brush sensorBrush, Brush sensorPen, int x, int y, int width, int height)
        {
            double thickness = 5;

            Ellipse ellipse = new Ellipse();
            /* 
             Define ellipse 
            */
            canvas.Children.Add(ellipse);
        }
    }   
}   

This was my first attempt, which throws up errors around the final canvas.Children line, namely "The name 'canvas' does not exist in the current context" 这是我的第一次尝试,它会在最终的canvas.Children行周围抛出错误,即“当前上下文中不存在名称'canvas'”

I tried moving DrawEllipse to a separate class here, which throws up different errors 我尝试将DrawEllipse移动到一个单独的类,这会引发不同的错误

namespace ZoomPan
{   
    public class DisplayManager
    {
        protected void DrawIcon(Locale locale, Color color) 
        {
            Brush sensorBrush = new SolidColorBrush(color);
            Brush sensorOutline = new SolidColorBrush(Colors.Black);
            int x = locale.x;
            int y = locale.y;
            halfSize = 10;
            DrawEllipse(locale.Name, sensorBrush, sensorOutline, x - halfSize, y - halfSize, 2 * halfSize, 2 * halfSize);
        }   
    }

    public class MainWindow : Window
    {
        public void DrawEllipse(string name, Brush sensorBrush, Brush sensorPen, int x, int y, int width, int height)
        {
            double thickness = 5;

            Ellipse ellipse = new Ellipse();
            /* 
             Define ellipse 
            */
            canvas.Children.Add(ellipse);
        }
    }   
}   

These errors are around the call to DrawEllipse in the first class: 这些错误是在第一个类中调用DrawEllipse:

"The name 'DrawEllipse' does not exist in the current context" “当前上下文中不存在'DrawEllipse'这个名称”

and when I add MainWindow. 当我添加MainWindow。 before the DrawEllipse: 在DrawEllipse之前:

"An object reference is required for the non-static field, method, or property 'ZoomPan.MainWindow.DrawEllipse(string, System.Windows.Media.Brush, System.Windows.Media.Brush, int, int, int, int)'" “非静态字段,方法或属性'ZoomPan.MainWindow.DrawEllipse(字符串,System.Windows.Media.Brush,System.Windows.Media.Brush,int,int,int,int)需要对象引用“”

When I first played around with WPF, I could get it to display onto the canvas when the DrawEllipse method was inside the public class MainWindow:Window of the MainWindow.xaml.cs file, hence the second attempt above. 当我第一次使用WPF时,当DrawEllipse方法位于MainWindow.xaml.cs文件的公共类MainWindow:Window中时,我可以将它显示在画布上,因此上面的第二次尝试。

I feel I am missing something obvious, and couldn't find any exact dupes of this question on the site. 我觉得我错过了一些明显的东西,并且在网站上找不到任何确切的问题。

The other classes need to be passed the canvas object to be able to use it. 其他类需要传递canvas对象才能使用它。

Either pass the canvas to the DisplayManager through the constructor once so it can be stored and then used in all methods: 通过构造函数将画布传递给DisplayManager一次,这样它就可以存储,然后在所有方法中使用:

public class DisplayManager
{
    Canvas canvas;
    public DisplayManager(Canvas canvas)
    {
        this.canvas = canvas;
    }
}

Or pass it into each method each time as the first parameter: 或者每次将其作为第一个参数传递给每个方法:

public class DisplayManager
{
    public void DrawEllipse(Canvas canvas, string name, Brush sensorBrush, Brush sensorPen, int x, int y, int width, int height) 
    { 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM