我有一个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关闭之前返回到原始调用上下文。 –