我無法將彈出窗口的所有者設置爲應用程序主窗口。WPF + PRISM如何將彈出窗口的所有者作爲主窗口
在這裏,我做了什麼。
創建一個帶有自定義PopupWindow的主窗口。
<Window x:Class="MainWindow" ... <Window.Resources> <Style x:Key="MessageWindowStyle" TargetType="{x:Type Window}"> <Setter Property="Background" Value="Transparent" /> <Setter Property="WindowStyle" Value="None" /> <Setter Property="ResizeMode" Value="NoResize" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="ShowInTaskbar" Value="False"/> <Setter Property="AllowsTransparency" Value="True"/> </Style> </Window.Resources> <i:Interaction.Triggers> <interactionRequest:InteractionRequestTrigger SourceObject="{Binding MessageRequest, Mode=OneWay}"> <interactionRequest:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowStyle="{StaticResource MessageWindowStyle}" > <interactionRequest:PopupWindowAction.WindowContent> <controls:PageCustomPopup /> </interactionRequest:PopupWindowAction.WindowContent> </interactionRequest:PopupWindowAction> </interactionRequest:InteractionRequestTrigger> </i:Interaction.Triggers> ... </Window>
PageCustomPopup.xaml
這是彈出窗口基本XAML。
<UserControl x:Class="PageCustomPopup"
...
</UserControl>
- PageCustomPopup.xaml.cs
主窗口後面的代碼。
public InteractionRequest<Notification> MessageRequest { get; private set; } public MainWindow() { ... MessageRequest = new InteractionRequest<Notification>(); ... } void Button_OnClick(object sender, EventArgs e) { MessageRequest.Raise(new Notification(){ Title = "Title Text", Content = "Message to Display"}); }
在該頁面代碼隱藏,我試圖設置父,被裝載在彈出窗口時。但它拋出異常。
public partial class PageCustomPopup : UserControl, InteractionRequestAware, INotifyPropertyChanged
{
public PageCustomPopup()
{
Loaded += (sender, args) =>
{
try
{
var parentWindow = this.Parent as Window;
if (parentWindow != null)
{
// This line fails with "Cannot set the Owner after the window is displayed."
//parentWindow.Owner = Application.Current.MainWindow;
// First, default to the main window's dimensions
parentWindow.Width = Application.Current.MainWindow.Width;
parentWindow.Height = Application.Current.MainWindow.Height;
parentWindow.Left = Application.Current.MainWindow.Left;
parentWindow.Top = Application.Current.MainWindow.Top;
}
}
}
}
}
當顯示自定義彈出窗口,它正確地顯示在主窗口的中心。
但問題是,當所有的窗口都被最小化時,如果點擊任務欄上的應用程序圖標,應用程序主窗口將顯示出來並顯示。無法選擇任何東西。
彈出式窗口被隱藏起來,不能放到前面。
將Popup窗口放在前面的唯一方法是使用「Alt + Tab」鍵。
我的問題是,如何將自定義彈出窗口的所有者設置爲Application.Current.MainWindow? 這樣,通過選擇任務欄圖標或Alt + Tab,顯示主窗口和彈出窗口。
謝謝Szabolcs !!!。此解決方案完美運作。 – ATati
@ATati你可以請upvote並接受答案。 –