我正在为 .NET 使用 Delphi Prism。我需要从另一个 winform 方法调用我的 mainform 类中的公共方法。所以,最近了解了静态,我在我的程序中使用了它。静态或类 winform 效果很好,但将方法设为静态或类似乎不一样。
我的主窗体类中有一个名为 updateButtons 的方法。它根据用户的操作更新主窗体上的所有按钮和控件。此方法需要从另一个 winform 方法调用。因此,我将 UpdateButtons 方法设为静态或类。虽然现在我看到了要调用的方法,但编译器不喜欢。它不断引发以下错误,“无法在没有实例引用的情况下调用实例成员(任何控件)。”
如何使方法成为类或静态方法,并且仍然可以从 winform 访问控件?
具有静态或类方法的主类:
MainForm = partial class(System.Windows.Forms.Form)
private
protected
method Dispose(disposing: Boolean); override;
public
class method updateButtons;
end;
更新按钮的定义:
class method MainForm.updateButtons;
begin
if SecurityEnabled then
LoginBtn.Enabled := true //All the lines where I call Buttons raise the error exception that I mentioned above.
else
begin
UnitBtn.Enabled := true;
SignalBtn.Enabled := true;
AlarmBtn.Enabled := true;
MakerBtn.Enabled := true;
TrendBtn.Enabled := true;
DxCommBtn.Enabled := (Scanning = false);
TxBtn.Enabled := true;
ControlBtn.Enabled := true;
PIDBtn.Enabled := true;
SystemBtn.Enabled := true;
WinListBox.Enabled := true;
WinBtn.Enabled := true;
ShutdownBtn.Enabled := true;
OptionBtn.Enabled := true;
LoginBtn.Enabled:=false;
end;
end;