gpt4 book ai didi

C#:如何访问对象实例?

转载 作者:行者123 更新时间:2023-11-30 16:32:56 24 4
gpt4 key购买 nike

我创建了一个带有计时器的 splash 类,当计时器结束时,它会实例化另一个类,如下面的代码所示。当我创建一个新类时,我如何访问 MainWindow?

namespace Kinetics
{
public class KineticsCommand : RMA.Rhino.MRhinoCommand
{
Splash Splash = new Splash();
Splash.Show();
}

public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
this.Close();

MainUI MainWindow = new MainUI();
MainWindow.Show();
}
}

public class CustomEventWatcher : MRhinoEventWatcher
{
public override void OnReplaceObject(ref MRhinoDoc doc, ref MRhinoObject old, ref MRhinoObject obj)
{
// How can i access the class from here?
}
}
}

最佳答案

CustomEventWatcher 需要引用 MainWindow,例如通过 CustomEventWatcher 中的属性:

public class CustomEventWatcher : MRhinoEventWatcher
{
MainUI _mainUI = null;
public MainUI MainWindow { get { return _mainUI; } set { _mainUI = value; } }

public override void OnReplaceObject(ref MRhinoDoc doc, ref MRhinoObject old, ref MRhinoObject obj)
{
if(_mainUI != null)
_mainUI.Whatever();
}
}

public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
this.Close();

MainUI MainWindow = new MainUI();
CustomEventWatcher cew = new CustomEventWatcher();
cew.MainWindow = MainWindow;
MainWindow.Show();
}
}

关于C#:如何访问对象实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3667695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com