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 オブジェクトへの参照が必要です。それらは常に 1 つしかないため、ごまかすことができます。

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

これで、instance.SplashScreen を使用して常にスプラッシュ スクリーンへの参照を取得し、SetSplashInfo() を静的にすることができます。完全な修正は可能ですが、SingleInstanceApplication インスタンスの作成方法がわかりません。

于 2010-12-16T15:32:44.017 に答える