5

私は、すべてのモーダルスタイルのダイアログがUserControlメインをカバーする半透明のグリッドの上に実装されるアプリケーション(c#+ wpf)を書いていますWindow。これは、1つだけでありWindow、すべての企業アプリケーションのルックアンドフィールを維持することを意味します。

を表示するMessageBoxための構文は次のとおりです。

CustomMessageBox b = new CustomMessageBox("hello world");
c.DialogClosed += ()=>
{
   // the rest of the function
}
// this raises an event listened for by the main window view model,
// displaying the message box and greying out the rest of the program.
base.ShowMessageBox(b); 

ご覧のとおり、実行の流れが実際に逆転しているだけでなく、従来の.NETバージョンと比較してひどく冗長です。

MessageBox.Show("hello world");
// the rest of the function

私が本当に探しているのはbase.ShowMessageBox、ダイアログを閉じるイベントが発生するまで戻らない方法ですが、GUIスレッドをハングさせて、ユーザーが[OK]をクリックするのを防ぐことなく、これを待つことができる方法がわかりません。デリゲート関数を関数のパラメーターとして使用して、ShowMessageBox実行の反転を防ぐことができますが、それでもいくつかのクレイジーな構文/インデントが発生することは承知しています。

私は明らかな何かを見逃していますか、それともこれを行うための標準的な方法がありますか?

4

4 に答える 4

5

CodeProjectのこの記事とMSDNのこの記事をご覧ください。最初の記事では、ブロッキングモーダルダイアログを手動で作成する方法を説明し、2番目の記事では、カスタムダイアログを作成する方法を説明します。

于 2009-12-01T15:17:06.803 に答える
4

これを行う方法は、 DispatcherFrameオブジェクトを使用することです。

var frame = new DispatcherFrame();
CustomMessageBox b = new CustomMessageBox("hello world");
c.DialogClosed += ()=>
{
    frame.Continue = false; // stops the frame
}
// this raises an event listened for by the main window view model,
// displaying the message box and greying out the rest of the program.
base.ShowMessageBox(b);

// This will "block" execution of the current dispatcher frame
// and run our frame until the dialog is closed.
Dispatcher.PushFrame(frame);
于 2015-03-30T06:58:15.110 に答える
0

メッセージボックスクラスに別のメッセージループを設定します。何かのようなもの :

public DialogResult ShowModal()
{
  this.Show();

  while (!this.isDisposed)
  {
    Application.DoEvents();
  } 

   return dialogResult;
}

ReflectorでWindows.Formを見ると、次のようになっていることがわかります。

于 2009-12-01T15:17:52.887 に答える
0

関数を、を返すイテレータにしてIEnumerator<CustomMessageBox>、次のように記述できます。

//some code
yield return new CustomMessageBox("hello world");
//some more code

次に、ハンドラーで列挙子と呼び出しMoveNext(次の関数まですべての関数を実行します)を受け取るラッパー関数を記述します。yield returnDialogClosed

ラッパー関数はブロッキング呼び出しではないことに注意してください。

于 2009-12-01T15:08:44.363 に答える