محاكاة وظيفة showdialog.
-
11-09-2019 - |
سؤال
أنا أكتب تطبيقا (C # + WPF) حيث يتم تنفيذ جميع مربعات حوار النمط الوسائط ك UserControl
على رأس شبكة شفافة تغطي الرئيسية Window
. وبعد هذا يعني وجود واحد فقط 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
حتى يتم رفع هذا الحدث مغلق الحوار، ولكن لا أستطيع أن أرى كيف يمكن أن تنتظر هذا دون شنقا موضوع واجهة المستخدم الرسومية وبالتالي منع المستخدم على الإطلاق بالنقر فوق "موافق". أنا أدرك أنني أستطيع أن تأخذ وظيفة مندوب كمعلمة إلى ShowMessageBox
وظيفة ما يمنع انقلاب التنفيذ، ولكن لا يزال يتسبب في بعض بناء الجملة مجنون / المسافة البادئة.
هل أفتقد شيئا واضحا أم أن هناك طريقة قياسية للقيام بذلك؟
نصائح أخرى
الطريقة للقيام بذلك هي باستخدام prodatcherframe. هدف.
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);
يمكنك أن تجعل وظيفتك إلى جهاز كمتكر IEnumerator<CustomMessageBox>
, ، ثم اكتبها مثل هذا:
//some code
yield return new CustomMessageBox("hello world");
//some more code
سوف تكتب بعد ذلك وظيفة التفاف التي تأخذ العداد والمكالمات MoveNext
(والتي ستنفذ كل الوظيفة حتى التالي yield return
) في ال DialogClosed
معالجات.
لاحظ أن وظيفة التفاف لن تكون مكالمة حظر.
قم بإعداد حلقة رسائل أخرى في فئة مربع الرسالة. شيء مثل :
public DialogResult ShowModal()
{
this.Show();
while (!this.isDisposed)
{
Application.DoEvents();
}
return dialogResult;
}
إذا نظرت إلى Windows.Form في العاكس، فسترى أنه شيء مثل هذا ..