-1

我们要触发事件,事件名称保存在 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

4

2 回答 2

0

明白了,现在工作正常!拉皮条棱镜库,按类型获取事件:-)

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

感谢你们!

于 2012-03-09T09:02:17.260 回答
0

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

于 2012-03-08T10:08:27.957 回答