2016-11-07 8 views
0

ロード中にプログラムが反応しないようにプログラム内から表示される読み込み画面がありますが、loadingScreen.Show();this.Hide();を使用すると読み込み画面に問題はありませんMahApps.MetroのGUI要素は表示されず、ラベルも表示されません。ここでWPFプログラムでGUI要素が表示されない

は、私がこれまで持っているコードは次のとおりです。

LoadingScreen screen = new LoadingScreen(); 
screen.InitializeComponent(); 
this.Hide(); 
screen.Show(); 

してからロードする必要があるもの、そして最後に

screen.Hide(); 
this.Show(); 
+0

? –

答えて

0

私はあなたがスレッドの問題を持っていると思います。あなたのスプラッシュ画面はメインスレッドをロックしており、メインアプリケーションには決して行きません。ここで私はこの問題をどのように解決しましたか。スプラッシュ画面と初期化用の新しいスレッドを作成しました。私はManualResetEventを使用して、初期化が完了し、メインアプリケーションが処理を進めることができるときに、メインスレッドに戻ってきます。いくつかの既存の忙しい指標を使用しないのはなぜ

public partial class App : Application 
{ 
    private static LoadingScreen splashScreen; 
    private static ManualResetEvent resetSplash; 

    [STAThread] 
    private static void Main(string[] args) 
    { 
     try 
     { 
      resetSplash = new ManualResetEvent(false); 
      var splashThread = new Thread(ShowSplash); 
      splashThread.SetApartmentState(ApartmentState.STA); 
      splashThread.IsBackground = true; 
      splashThread.Name = "My Splash Screen"; 
      splashThread.Start(); 
      resetSplash.WaitOne(); //wait here until init is complete 

      //Now your initialization is complete so go ahead and show your main screen 
      var app = new App(); 
      app.InitializeComponent(); 
      app.Run(); 
     } 
     catch (Exception ex) 
     { 
      //Log it or something else 
      throw; 
     } 
    } 

    private static void ShowSplash() 
    { 
     splashScreen = new LoadingScreen(); 
     splashScreen.Show(); 
     try 
     { 
      //this would be your async init code inside the task 
      Task.Run(async() => await Initialization()) 
      .ContinueWith(t => 
       { 
        //log it 
       }, TaskContinuationOptions.OnlyOnFaulted); 
    } 
    catch (AggregateException ex) 
    { 
     //log it 
    } 
    resetSplash.Set(); 
    Dispatcher.Run(); 
} 

}

+0

申し訳ありませんが、忘れてしまった;これはボタンのクリックハンドラから呼び出される必要があります。私はこれに似た何かをしますか? – John

+0

私はまだそれと同じようにやります。違いは、それがメインにあるのではなく、ボタンのクリックで同じことをすることです。 var window = new MyWindowClass(); '' '' '' window.Show(); '' 'のように' 'var app = new App();' – phil

関連する問題