InteractionRequestTrigger PopupWindowAction自定义图标

时间:2015-05-25 13:22:34

标签: wpf prism

我正在使用PRISM 5,我正在使用InteractionRequestTriggerPopupWindowAction展示自定义视图。
有没有人知道如何设置对话窗口的图标?

4 个答案:

答案 0 :(得分:3)

注意:这仅适用于Prism 6及以上

<i:Interaction.Triggers>
    <prism:InteractionRequestTrigger SourceObject="{Binding SomeInteractionRequest, Mode=OneWay}" >
        <prism:PopupWindowAction>
            <prism:PopupWindowAction.WindowStyle>
                <Style TargetType="Window">
                    <Setter Property="Icon" Value="put here your icon"/>
                </Style>
            </prism:PopupWindowAction.WindowStyle>
            <prism:PopupWindowAction.WindowContent>
                <!-- put your own view here -->
                <local:SettingsWindowView />
            </prism:PopupWindowAction.WindowContent>
        </prism:PopupWindowAction>
    </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>

其中名称空间

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:prism="http://prismlibrary.com/"

并且无需从PopupWindowAction

创建子类

答案 1 :(得分:1)

我自己找到了答案,解决方案是(我认为)从 PopupWindowAction 派生一个具有新图标DependencyProperty的类。我还添加了一个断言来检查notification.Title是null还是空,在这种情况下我将wrapperWindow.Title设置为空字符串。

using System.Windows;
using System.Windows.Media;
using Microsoft.Practices.Prism.Interactivity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;

/// <summary>
/// Shows a popup window with in response to an <see cref="InteractionRequest"/> being raised.
/// </summary>
/// <remarks>
/// This popup window has an Icon property that can be used to set the icon
/// </remarks>
public class IconPopupWindowAction : PopupWindowAction
{
    /// <summary>
    /// The icon of the child window that is displayed as part of the popup.
    /// </summary>
    public static readonly DependencyProperty IconProperty =
       DependencyProperty.Register(
        "Icon",
        typeof(ImageSource),
        typeof(IconPopupWindowAction),
        new PropertyMetadata(null));

    /// <summary>
    /// Gets or sets the Icon of the window.
    /// </summary>
    public ImageSource Icon
    {
        get { return (ImageSource)this.GetValue(IconProperty); }
        set { this.SetValue(IconProperty, value); }
    }

    /// <summary>
    /// Returns the window to display as part of the trigger action.
    /// </summary>
    /// <param name="notification">The notification to be set as a DataContext in the window.</param>
    /// <returns>
    /// The popup window
    /// </returns>
    protected override Window GetWindow(INotification notification)
    {
        Window wrapperWindow;

        if (this.WindowContent != null)
        {
            wrapperWindow = new Window();

            // If the WindowContent does not have its own DataContext, it will inherit this one.
            wrapperWindow.DataContext = notification;

            if (string.IsNullOrEmpty(notification.Title))
            {
                wrapperWindow.Title = string.Empty;
            }
            else
            {
                wrapperWindow.Title = notification.Title;
            }

            wrapperWindow.Icon = this.Icon;

            this.PrepareContentForWindow(notification, wrapperWindow);
        }
        else
        {
            wrapperWindow = this.CreateDefaultWindow(notification);
            wrapperWindow.Icon = this.Icon;
        }

        return wrapperWindow;
    }
}

答案 2 :(得分:1)

一个非常简单的解决方案可能是

public class IconPopupWindowAction : PopupWindowAction
{
    public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(IconPopupWindowAction), new PropertyMetadata(null));

    public ImageSource Icon
    {
        get { return (ImageSource)this.GetValue(IconProperty); }
        set { this.SetValue(IconProperty, value); }
    }

    protected override Window GetWindow(INotification notification)
    {
        Window defaultWindow = base.GetWindow(notification);
        defaultWindow.Icon = this.Icon;
        return defaultWindow;
    }
}

这也更好,因为您提供的解决方案是Prism implementation of this method的复制和粘贴,但您稍微修改了一下。
不幸的是,棱镜的实现可以改变_ 实际上,从你复制它的时候它真的发生了变化 _
因此,按照您的方式,您必须更新IconPopupWindowAction课程以与Prism保持同步。 此解决方案更好,因为它不是从Prism库中复制代码,而是通过调用基本方法base.GetWindow(notification)真正重用它
当您将Prism更新为更新版本时,这可以防止您破坏Prism代码。

答案 3 :(得分:-1)

转到项目属性(从解决方案资源管理器中选择项目,然后单击 Alt + Enter

应用标签中,转到图标和清单,然后选择项目的图标,这会将图标设置为项目中的每个窗口。
并且不需要IconPopupWindowAction