C# 在Visual Studio 2013中使用画布
我对C#和VisualStudio2013相当陌生。我有一个相当大的项目要做,我不是唯一一个在工作。我们有多个类,我负责管理主窗口中的画布(画布可以是ShapeContainer类型,因为我主要是画圆和线)。但问题是:如果我的类试图绘制它的私有方法,它应该如何知道画布的存在?由于C# 在Visual Studio 2013中使用画布,c#,wpf,pointers,canvas,visual-studio-2013,C#,Wpf,Pointers,Canvas,Visual Studio 2013,我对C#和VisualStudio2013相当陌生。我有一个相当大的项目要做,我不是唯一一个在工作。我们有多个类,我负责管理主窗口中的画布(画布可以是ShapeContainer类型,因为我主要是画圆和线)。但问题是:如果我的类试图绘制它的私有方法,它应该如何知道画布的存在?由于System.Windows.Controls.Canvas对象是主窗口的子对象,我在主窗口中初始化了我的类的一个实例,因此我想给我的类一个指向画布的指针(就像我在C/C++中所做的那样),但是private Syste
System.Windows.Controls.Canvas
对象是主窗口的子对象,我在主窗口中初始化了我的类的一个实例,因此我想给我的类一个指向画布的指针(就像我在C/C++中所做的那样),但是private System.Windows.Controls.Canvas*C代码>为我提供了一个无法获取的地址、获取的大小或声明指向托管类型错误的指针的错误。我想这可能不是一个好办法,这就是为什么我决定问你们好的做法是什么?您将如何在应用程序中解决此问题?
另外,如果我的Class A
具有类型为B
的公共属性,然后在B
中,我将父属性设置为A
,这是如何相互作用的?这些属性中的一个必须被理解为指针,否则我们将有一个类在另一个类中的无限递归,或者我遗漏了什么?
提前谢谢。
MainWindow.xaml:
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Chrum graph" Height="525" Width="525" MinHeight="250" MinWidth="250">
<Grid>
<Canvas Name="VisualGraph" HorizontalAlignment="Stretch" Margin="0,20,0,0" VerticalAlignment="Stretch" Background="#AAAAAA"/>
</Grid>
</Window>
Core.cs:
public class Core
{
private IVisual visual;
}
Visual.cs:
public class Visual: IVisual
{
private void draw()
{
//this is where i want to use the canvas and draw in it
}
}
长话短说:
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Chrum graph" Height="525" Width="525" MinHeight="250" MinWidth="250">
<Grid>
<Canvas Name="VisualGraph" HorizontalAlignment="Stretch" Margin="0,20,0,0" VerticalAlignment="Stretch" Background="#AAAAAA"/>
</Grid>
</Window>
核心:
在托管类中不使用指针。C和C++是不一样的。除了得到一个指针,你为什么不描述一下你想做什么?你打算用指针(或画布)做什么?另外,你似乎在问C语言,Visual Studio只是一个IDE,它不会改变你与对象或类交互的方式,这似乎与此无关。我想在画布上绘制我的类Visual
的私有方法,它是类核心的子类
,它是主窗口的子类
。画布i也是主窗口的子窗口
,但显然画布在Visual
中不可见。实际上,这并不明显,没有任何代码显示这些类之间的关系……例如,如果您有画布,那么canvas=new canvas()
,如果您想在类函数中使用它,那么您应该能够提供canvas
作为函数的参数,即Visual.SomeFunction(canvas)代码>。对象总是在C++中通过ReF传递,所以传递对象非常类似于C++中传递指针。
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Chrum graph" Height="525" Width="525" MinHeight="250" MinWidth="250">
<Grid>
<Canvas Name="VisualGraph" HorizontalAlignment="Stretch" Margin="0,20,0,0" VerticalAlignment="Stretch" Background="#AAAAAA"/>
</Grid>
</Window>
public MainWindow()
{
InitializeComponent();
new Core(VisualGraph).DrawSomething();
}
class Core
{
private Canvas innerCanvas;
public Core(Canvas canvas)
{
this.innerCanvas = canvas;
}
public void DrawSomething()
{
Ellipse ellipse = new Ellipse();
ellipse.Fill = Brushes.Red;
ellipse.Width = 20;
ellipse.Height = 20;
ellipse.SetValue(Canvas.TopProperty,20d);
ellipse.SetValue(Canvas.LeftProperty,20d);
this.innerCanvas.Children.Add(ellipse);
}
}