我有一个WPF应用程序,希望创建一个具有模态行为的自定义弹出窗口。我已经能够使用等效于“DoEvents”的方法来解决问题,但是是否有更好的方法?以下是我目前的代码:
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是一个用户控件,旨在承载另一个具有动画和其他支持的元素。