Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
您好,我有一个带有 2 个窗口的 WPF/C# 应用程序。我正在尝试访问
public int myInt;
在我的 OtherWindow 的 MainWindow 中:
MainWindow.myInt =3;
智能感知甚至不允许我访问该变量。
有人可以帮忙吗?
您需要声明它,static因为您不通过实例访问它,而是通过类名访问它。也就是说,公开公开字段通常不被认为是好的设计。
static
您要么需要一个对象:
MainWindow mw = new MainWindow(); mw.myInt = 3
或者您需要将 rour 字段设为静态
public static int myInt;
并像您已经在做的那样称呼它: