我有一个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是一个用户控件,旨在承载另一个具有动画和其他支持的元素。
发布于 2010-03-28 02:44:10
我建议重新考虑这个设计。
在某些情况下,使用"DoEvents“可能会导致一些非常奇怪的行为,因为您允许代码运行,同时试图阻止它。
除了使用弹出式窗口,你可以考虑使用一个带有ShowDialog的窗口,并适当地设置它的样式。这将是实现模式行为的“标准”方式,WPF允许您轻松地将窗口样式设置为与弹出窗口完全相同……
https://stackoverflow.com/questions/2530087
复制相似问题