我想在主窗口中添加绘图 但是当我试图访问窗口小部件时,它说:
错误CS0120:访问非静态需要对象引用 成员'MainWindow.win'
有人能解释这意味着什么,以及如何解决这个问题?
这是代码:
using Gtk;
using System;
using Cairo;
public class MainWindow {
public Window win = new Window("Traffic Light Simulator 0.1");
Fixed winFix = new Fixed();
DrawingArea rightSeparator = new DrawingArea();
MainWindow() {
win.SetSizeRequest(800, 600);
win.Resizable = false;
win.DeleteEvent += delegate { Application.Quit(); };
win.SetPosition(WindowPosition.Center);
win.ModifyBg(StateType.Normal, new Gdk.Color(255, 255, 255));
rightSeparator.SetSizeRequest(10, 600);
rightSeparator.ModifyBg(StateType.Normal, new Gdk.Color(200, 200, 200));
winFix.Put(rightSeparator, 580, 0);
win.Add(winFix);
win.ShowAll();
}
public static void Main() {
Application.Init();
new MainWindow();
Application.Run();
}
}
public class Light {
DrawingArea darea = new DrawingArea();
Light(int x, int y, int size) {
MainWindow.win.Add(darea);
}
}
答案 0 :(得分:1)
似乎对你想做的事情存在误解:
Add()
要解决此问题,您需要保留对主窗口的引用:
new MainWindow();
应为MainWindow myMainWindow=new MainWindow();
Light(int x, int y, int size) { MainWindow.win.Add(darea); }
应为Light(MainWindow theMainWindow, int x, int y, int size) { theMainWindow.win.Add(darea); }
Light()