使用DialogService 弹窗时,丢失Active 状态的问题

在WPF中使用Prism框架提供的DialogService弹窗时(尤其时在弹窗中再次弹窗时),在最上层弹窗关闭时引起激活主窗体或其他打开的程序,导致弹窗或主窗体被新弹出的窗体遮挡。

问题是由弹窗Ower丢失引起的问题。

 

解决方案为在 DialogWindow的 ’ Closing  ‘ 和 ’ Closed ‘ 事件中设置 Owner?.Activate();

 

参考代码

 1  public class DialogWindow : Window, IDialogWindow
 2     {
 3         public DialogWindow() : this(0) { }
 4 
 5         public DialogWindow(int captionHeight)
 6         {
 7             ShowInTaskbar = false;
 8             WindowStartupLocation = WindowStartupLocation.CenterOwner;
 9             Owner = Application.Current.MainWindow;
10             WindowState = WindowState.Normal;
11             SizeToContent = SizeToContent.WidthAndHeight;
12             WindowStyle = WindowStyle.None;
13             AllowsTransparency = true;
14             ResizeMode = ResizeMode.NoResize;
15             Background = Brushes.Transparent;
16             WindowChrome.SetWindowChrome(this, new WindowChrome
17             {
18                 CaptionHeight = captionHeight,
19                 ResizeBorderThickness = new Thickness(1),
20                 CornerRadius = new CornerRadius(6),
21                 GlassFrameThickness = new Thickness(0),
22                 UseAeroCaptionButtons = false,
23                 NonClientFrameEdges = NonClientFrameEdges.None
24             });
25             Closing += DialogWindow_Closing;
26             Closed += DialogWindow_Closed;
27         }
28 
29 
30         private void DialogWindow_Closing(object sender, EventArgs e) => Owner?.Activate();
31 
32         private void DialogWindow_Closed(object sender, EventArgs e)
33         {
34             Closing -= DialogWindow_Closing;
35             Closed -= DialogWindow_Closed;
36         }
37 
38         public IDialogResult Result { get; set; }
39     }
40 }

 

posted @ 2021-12-25 13:59  胖子说嘛  阅读(182)  评论(0)    收藏  举报