通用EventAggregator?

时间:2012-03-08 09:52:37

标签: wpf generics mvvm prism eventaggregator

我们希望触发事件,事件名称保存在SQL Server

在SQL Server中,您将找到ApplicationExitRequestEvent 当我们单击菜单按钮时,我们将从MenuItem

中获取字符串
Type t = Type.GetType(SelectedMenu.View + "," + "AssemblyName");
var obj = Activator.CreateInstance(t);

if (t != null)
{
//Working firing Event with className
EventAggregator.GetEvent<ApplicationExitRequestEvent>().Publish(null);

//Generic?
EventAggregator.GetEvent<???>().Publish(null);
}

我有可能做到吗? 使用PRISM和MVVM - WPF - .NET 4.0

2 个答案:

答案 0 :(得分:0)

如果你看一下EventAggregator类,你会发现它只不过是一个容器Dictionary<Type, EventBase>GetEvent方法。就是这样,所有实际工作都在EventBase完成。为了实现你想要的,你可以修改类(或复制并修改它),并添加一个方法GetEvent( string typeString ),在其中将typeString转换为实际的Type(与在你的代码示例中)并使用它来从字典中获取事件。

答案 1 :(得分:0)

知道了,现在工作正常! Pimped the Prism Library,通过Type :-)

获得活动
    /// <summary>
    /// Gets the single instance of the event managed by this EventAggregator. 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
    public CompositePresentationEvent<object> GetEvent(Type type)
    {
        EventBase existingEvent = null;
        events.TryGetValue(type, out existingEvent);

        if(existingEvent != null)
            return (CompositePresentationEvent<object>)existingEvent;

        return null;
    }

谢谢你们!