问题描述
我试图用C#语言编写一个函数,将椭圆绘制到在单独的Window1.xaml文件中创建的画布(canvas1).我尝试通过以下方式将Ellipse(node)添加到画布时出现问题: canvas1.Children.Add(node);
canvas1在当前上下文中不存在.我需要知道如何在这种情况下访问canvas1.
I am trying to write a function in C# to draw an Ellipse to a canvas (canvas1) that I have created in a separate Window1.xaml file. My problem arises when I try to add the Ellipse(node) to the canvas via: canvas1.Children.Add(node);
canvas1 does not exist in the current context. I need to know how to access canvas1 in this context.
推荐答案
public void AddEllipse(Canvas canvas)
{
...
...
canvas.Children.Add(node);
}
如果从其他代码中调用函数,则可以将画布公开为Window1的公共属性.
If your function is being called from some other code you can expose the canvas as a public property of Window1.
public Canvas Canvas1
{
get
{
return canvas1;
}
}
然后,通过Window1的任何实例,您都可以访问Canvas1属性并将其传递给函数.
Then through any instance of Window1 you can access the Canvas1 property and pass it to your function.
这篇关于如何从单独的类文件向WPF画布添加元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!