我应该在哪里创建可供其他类方法使用的对象?
我不确定在哪里创建 SolidColorBrush 对象。它们应该 a) 公共 MainWindow() 初始化方法中,b) 直接在 MainWindow 类中,还是 c) 在不同的新方法中?
在 public MainWindow() 中创建了一些 Ellipse 对象,并且 SetEllipsePosition 访问它们没有问题(可能是因为 Ellipse 是作为参数传入的?)。但是,在公共 MainWindow() 中创建的 SolidColorBrush 对象对 SetEllipsePosition() 不可见。
当我直接在类中创建这些对象时(在“Kinect Runtime”之后),SetEllipsePosition() 会看到它们。但这是不好的风格吗?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Ellipse headEllipse = new Ellipse();
Ellipse leftEllipse = new Ellipse();
Ellipse rightEllipse = new Ellipse();
SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green); // where should these
SolidColorBrush redBrush = new SolidColorBrush(Colors.Red); // objects be defined?
SolidColorBrush orangeBrush = new SolidColorBrush(Colors.Orange);
SolidColorBrush yellowBrush = new SolidColorBrush(Colors.Yellow);
}
//Kinect Runtime
Runtime nui = new Runtime();
private void SetEllipsePosition(FrameworkElement ellipse, Joint joint)
{
...
(ellipse as Ellipse).Fill = greenBrush;
MainCanvas.Background = orangeBrush;
...
}
}
I'm not sure where to create the SolidColorBrush objects. Should they go a) inside the public MainWindow() initialization method, b) directly in the MainWindow class, or c) in a different, new method?
A few Ellipse objects are created in public MainWindow(), and SetEllipsePosition has no problem accessing them (maybe because Ellipses are passed in as an arg?). However, SolidColorBrush objects created in public MainWindow() aren't visible to SetEllipsePosition().
When I create these objects directly in the class (after 'Kinect Runtime'), SetEllipsePosition() sees them. But is this bad style?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Ellipse headEllipse = new Ellipse();
Ellipse leftEllipse = new Ellipse();
Ellipse rightEllipse = new Ellipse();
SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green); // where should these
SolidColorBrush redBrush = new SolidColorBrush(Colors.Red); // objects be defined?
SolidColorBrush orangeBrush = new SolidColorBrush(Colors.Orange);
SolidColorBrush yellowBrush = new SolidColorBrush(Colors.Yellow);
}
//Kinect Runtime
Runtime nui = new Runtime();
private void SetEllipsePosition(FrameworkElement ellipse, Joint joint)
{
...
(ellipse as Ellipse).Fill = greenBrush;
MainCanvas.Background = orangeBrush;
...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法访问画笔的原因是因为它们是在构造函数 MainWindow 中创建的,如果您在类本身中有一个
private SolidColorBrush greenBrush;
,然后在 MainWindow() 中添加一个实例,例如现在您就可以在SetEllipsePosition
中访问它。The reason why you can't access the brushes is because they are created in the constructor MainWindow, if you but a
private SolidColorBrush greenBrush;
in the class itself and then in the MainWindow() add an instance like you do now you'll be able to access it inSetEllipsePosition
.在类级别中删除变量并在 ctor 中实例化它始终是最佳实践。在您的情况下,SolidColorBrush 对象是 ctor 私有的。那么老友有什么用呢?
Its always best practice to delcare variable in class level and instantiate it in ctor. In your case, SolidColorBrush objects are private to ctor. So whats the use mate?