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
대화 상자 닫힘 이벤트가 발생할 때까지 GUI 스레드를 정지시켜 사용자가 확인을 클릭하지 못하게 하지 않고 이를 기다리는 것이 어떻게 가능한지 알 수 없습니다.나는 대리자 함수를 매개변수로 사용할 수 있다는 것을 알고 있습니다. ShowMessageBox
실행 반전을 방지하지만 여전히 이상한 구문/들여쓰기를 유발하는 함수입니다.
제가 뭔가 분명한 것을 놓치고 있는 건가요? 아니면 이를 수행하는 표준 방법이 있나요?
다른 팁
이를 수행하는 방법은 a를 사용하는 것입니다 디스패처 프레임 물체.
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에서 반사판을 보면 이런 일이 있습니다 ..