我有一個WPF應用程序,我想創建一個具有模態行爲的自定義彈出窗口。我已經能夠使用相當於「DoEvents」的解決方案來破解解決方案,但有沒有更好的方法來做到這一點?這是我目前所擁有的:WPF中的自定義模態窗口?
private void ShowModalHost(FrameworkElement element)
{
//Create new modal host
var host = new ModalHost(element);
//Lock out UI with blur
WindowSurface.Effect = new BlurEffect();
ModalSurface.IsHitTestVisible = true;
//Display control in modal surface
ModalSurface.Children.Add(host);
//Block until ModalHost is done
while (ModalSurface.IsHitTestVisible)
{
DoEvents();
}
}
private void DoEvents()
{
var frame = new DispatcherFrame();
Dispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(ExitFrame), frame);
Dispatcher.PushFrame(frame);
}
private object ExitFrame(object f)
{
((DispatcherFrame)f).Continue = false;
return null;
}
public void CloseModal()
{
//Remove any controls from the modal surface and make UI available again
ModalSurface.Children.Clear();
ModalSurface.IsHitTestVisible = false;
WindowSurface.Effect = null;
}
我的ModalHost是一個用戶控件,用於託管另一個具有動畫和其他支持的元素。
是的,這是應該如何完成的。除此之外,你可能不需要IsHitTestVisible循環。 – Ray
該循環是什麼使ShowModalHost呼叫阻塞;否則它將在ModalHost關閉之前返回到原始調用上下文。 –