首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何以编程方式将TypedEventHandler添加到RoutedEvents?

如何以编程方式将TypedEventHandler添加到RoutedEvents?
EN

Stack Overflow用户
提问于 2015-07-07 15:35:03
回答 1查看 2.2K关注 0票数 3

我正在研究一种扩展方法,其中一部分存在一些问题。

这就是我的想法:我想要创建一个方法,它接受一个对象和一个事件的名称,等待事件被引发,然后返回它的事件args。每次我在代码中这样做时,我都会使用它来避免手动创建委托和其他东西。

注意:代码位于针对Windows8.1和Windows8.1 8.1的PCL中,因此这里没有可用的反射方法。

到目前为止,这就是我所拥有的:

代码语言:javascript
运行
复制
/// <summary>
/// Waits for an event to be raised and returns its arguments
/// </summary>
/// <typeparam name="Targs">The type of the event args to return</typeparam>
/// <param name="target">The target object that will raise the event</param>
/// <param name="eventName">The name of the event to wait</param>
/// <param name="timeout">The time limit (in millisecond) to wait for the desired event to be raised</param>
public static async Task<Targs> WaitEventAsync<Targs>(this object target, String eventName, int timeout)
    where Targs : class
{
    // Arguments check
    if (target == null) throw new ArgumentNullException("The target object can't be null");
    if (eventName == null) throw new ArgumentNullException("The event name can't be null");
    if (timeout <= 0) throw new ArgumentOutOfRangeException("The timeout must be greater than 0");

    // Get the target event
    EventInfo eventInfo = target.GetType().GetRuntimeEvent(eventName);
    if (eventInfo == null) throw new ArgumentException(String.Format("The target object doesn't contain the {0} event", eventName));

    // Prepare the TaskCompletionSource, the return variable and the right handler
    TaskCompletionSource<Targs> tcs = new TaskCompletionSource<Targs>();

    Delegate handler;

    if (eventInfo.EventHandlerType.Equals(typeof(EventHandler<Targs>)))
    {
        handler = new EventHandler<Targs>((sender, args) =>
        {
            tcs.SetResult(args);
        });
    }
    else
    {
        // PROBLEM: when this line is executed, the AddEventHandler method crashes
        handler = new TypedEventHandler<object, Targs>((sender, args) => 
       { 
           tcs.SetResult(args); 
       });
    }

    // Add the handler and wait for the event
    eventInfo.AddEventHandler(target, handler);
    CancellationTokenSource cts = new CancellationTokenSource(timeout);
    try
    {
        // If the event was triggered before the timout expired, return its args
        return await tcs.Task.GetWatchedTask(cts);
    }
    catch (OperationCanceledException)
    {
        // If the timout expired, just return null
        return null;
    }
    finally
    {
        // Remove the handler from the target object
        eventInfo.RemoveEventHandler(target, handler);
    }
}

现在,所有的事情都很适合标准事件,所以这里没有问题。但是,当我有了RoutedEvents时,我就得到了一个例外。最后,我尝试使用TypedEventHandler类,因为我没有找到其他方法为这些事件获得正确的委托(就像所有指针事件一样)。但是,当我尝试添加处理程序时,我会得到一个InvalidOperationException

在运行时为RoutedEvents创建处理程序也是可能的,还是在WinRT上不可能?

谢谢你的帮忙!

塞尔吉奥

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-10 15:19:14

这是因为WinRT不允许您使用AddHandler方法添加事件处理程序。我知道可以这样做的唯一方法是使用以下代码:

代码语言:javascript
运行
复制
    private void AddHandler(FrameworkElement element, object parameter, EventInfo eventInf)
    {
        var addMethod = eventInf.AddMethod;
        var removeMethod = eventInf.RemoveMethod;

        var addParameters = addMethod.GetParameters();
        var delegateType = addParameters[0].ParameterType;

        Action<object, object> handler = (s, e) => ExecuteCommand();
        var handlerInvoke = typeof(Action<object, object>).GetRuntimeMethod("Invoke", new[] { typeof(object), typeof(object) });

        var @delegate = handlerInvoke.CreateDelegate(delegateType, handler);

        Func<object, EventRegistrationToken> add = a => (EventRegistrationToken)addMethod.Invoke(element, new object[] { @delegate });
        Action<EventRegistrationToken> remove = t => removeMethod.Invoke(element, new object[] { t });

        WindowsRuntimeMarshal.AddEventHandler(add, remove, handler);
    }

若要删除处理程序,请使用以下命令:

代码语言:javascript
运行
复制
WindowsRuntimeMarshal.RemoveEventHandler(remove, handler);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31273109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档