本文介绍了WPF / EventSetter /强处理程序类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//EventSetter.Seal

if(this._handler.GetType()!= this._event.HandlerType)
{
抛出新的ArgumentException(System.Windows.SR.Get(" HandlerTypeIllegal"));
}


if (this._handler.GetType() != this._event.HandlerType){throw new ArgumentException(System.Windows.SR.Get("HandlerTypeIllegal"));}

为什么要进行严格比较测试?

Why is there a test for a strict comparison?

我认为,检查参数的兼容性就足够了。

I think , it is enough to check the compatibility of the parameters.

例如,可以从MouseEventArgs分配RoutedEventArgs。

RoutedEventArgs is assignable from MouseEventArgs for example.

推荐答案

>>为什么要进行严格比较测试?

>>Why is there a test for a strict comparison?

这是为了确保RoutedEvent的handlerType与委托处理程序的handlerType相同。

This is to ensure the handlerType of RoutedEvent is the same as the one of delegated handler.


EventSetter构造
,带有两个参数:

/// <summary>
///     EventSetter construction
/// </summary>
public EventSetter(RoutedEvent routedEvent, Delegate handler)
{
    if (routedEvent == null)
    {
        throw new ArgumentNullException("routedEvent");
    }
    if (handler == null)
    {
        throw new ArgumentNullException("handler");
    }
 
    _event = routedEvent;
    _handler = handler;
}

Style中EventSetter的用法:

The usage of EventSetter in Style:

<Style x:Key="ChangeBackgroundColor" TargetType="TextBlock">
        <EventSetter Event="TextBlock.MouseEnter" Handler="ChangeBackgroundColorOnMouseEnter" />
</Style>

通过使用该if子句,它将检查委托事件是否合法。

By using that if clause, it will check whether the delegated event is legal or not.


这篇关于WPF / EventSetter /强处理程序类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:12