1

在应用程序启动时更新启动画面标签以告知用户发生了什么的最佳方法是什么?问题是闪屏是在覆盖方法中创建的,而更新必须在静态主方法中完成,它无法访问“this.SplashScreen”。

class SingleInstanceApplication : WindowsFormsApplicationBase
{
    [STAThread]
    static void Main(string[] args)
    {
        SetSplashInfo("Data configuration", "Applying DataDirectory"); 
        //Can't be done, this method is static**
        //Do some stuff, code removed for reading purposes
    }

    protected override void OnCreateSplashScreen()
    {
        this.SplashScreen = new TestSplash();
        this.SplashScreen.TopMost = true; 

        base.OnCreateSplashScreen();
    }

    private void SetSplashInfo(string txt1, string txt2)
    {
        if (  this.SplashScreen == null)
            return;
       TestSplash splashFrm = (TestSplash)this.SplashScreen;
        splashFrm.label1.Text = txt1;
        splashFrm.label2.Text = txt2;
    }
}
4

1 回答 1

2

是的,您需要引用 SingleInstanceApplication 对象。由于只有其中一个,您可以作弊:

class SingleInstanceApplication : WindowsFormsApplicationBase {
    private static SingleInstanceApplication instance;
    public SingleInstanceApplication() {
       instance = this;
    }
}

现在您可以使用 instance.SplashScreen 始终获取对初始屏幕的引用并使 SetSplashInfo() 静态。应该可以进行干净的修复,但我看不到您是如何创建 SingleInstanceApplication 实例的。

于 2010-12-16T15:32:44.017 回答