WPF + PRISM如何使弹出窗口的所有者成为主窗口 [英] WPF+PRISM How to make the popup window owner as main window

查看:159
本文介绍了WPF + PRISM如何使弹出窗口的所有者成为主窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将弹出窗口"的所有者设置为应用程序主窗口".

I am having trouble setting the owner of Popup window to Application Main window.

这就是我所做的.

  1. 使用自定义PopupWindow创建了一个主窗口.

  1. Created a Main Window with custom 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

  • PageCustomPopup.xaml

    这是弹出窗口的基本xaml.

    This is basic xaml for the popup window.

        <UserControl x:Class="PageCustomPopup"
            ...
        </UserControl>
    

    1. PageCustomPopup.xaml.cs

    在此代码隐藏页面中,我尝试在加载弹出窗口时设置父级.但这会引发异常.

    In this page code-behind, I tried to set the parent, when the popup window is loaded. But it throws exception.

        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;
                        }
                    }
                }
            }
        }
    

    1. 后面的MainWindow代码.

    1. MainWindow code behind.

    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"});
    }
    

  • 显示自定义"弹出窗口时,它会正确显示在主窗口的中央.

    When the Custom popup window is displayed, it shows correctly in the center of the main window.

    但是问题是,当所有窗口都最小化时,如果单击任务栏上的应用程序"图标,则会显示应用程序"主窗口,并且该窗口无效.无法选择任何内容.

    But the issue is, when all the windows are minimized, if the Application icon on the taskbar is clicked, the Application main window is displayed, and inavtive. Cannot select anything.

    弹出窗口被隐藏,无法显示在前面.

    The popup window is hidden and cannot bring to front.

    将弹出窗口置于最前面的唯一方法是使用"Alt + Tab"键.

    The only way to bring the Popup window in front, is using "Alt+Tab" keys.

    我的问题是,如何将自定义"弹出窗口所有者设置为Application.Current.MainWindow? 这样,通过选择任务栏图标或Alt + Tab,可以同时显示Mainwindow和弹出窗口.

    My question is, how to set the Custom popup window owner to Application.Current.MainWindow? So that, by selecting taskbar icon or Alt+Tab, displays both Mainwindow and popup window.

    推荐答案

    同时,如果您不想使用预览版本,则可以自己实现,它源自PopupWindowAction:

    In the meantime if you don't want to use a preview version, you can just implement this yourself deriving from PopupWindowAction:

    public class PopupChildWindowAction : PopupWindowAction
    {
        public static readonly DependencyProperty WindowOwnerProperty = DependencyProperty.Register(
            "WindowOwner", typeof (Window), typeof (PopupChildWindowAction), new PropertyMetadata(default(Window)));
    
        public Window WindowOwner
        {
            get { return (Window) GetValue(WindowOwnerProperty); }
            set { SetValue(WindowOwnerProperty, value); }
        }
    
        protected override Window GetWindow(INotification notification)
        {
            Window wrapperWindow;
            if (this.WindowContent != null)
            {
                wrapperWindow = this.CreateWindow();
                if (wrapperWindow == null)
                    throw new NullReferenceException("CreateWindow cannot return null");
                wrapperWindow.Owner = WindowOwner;
                wrapperWindow.DataContext = (object)notification;
                wrapperWindow.Title = notification.Title;
                this.PrepareContentForWindow(notification, wrapperWindow);
            }
            else
                wrapperWindow = this.CreateDefaultWindow(notification);
            if (this.WindowStyle != null)
                wrapperWindow.Style = this.WindowStyle;
            return wrapperWindow;
        }
    }
    

    用法:

    <i:Interaction.Triggers>
        <interactionRequest:InteractionRequestTrigger SourceObject="{Binding MessageRequest, Mode=OneWay}">
            <local:PopupChildWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowStyle="{StaticResource MessageWindowStyle}" 
                                          WindowOwner="{Binding ElementName=MyMainWindow}">
                <interactionRequest:PopupWindowAction.WindowContent>
                    <local:PageCustomPopup />
                </interactionRequest:PopupWindowAction.WindowContent>
            </local:PopupChildWindowAction>
        </interactionRequest:InteractionRequestTrigger>
    </i:Interaction.Triggers>
    

    这篇关于WPF + PRISM如何使弹出窗口的所有者成为主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆