我编写的代码实际上是WPF行为,用于从网格控件中获取选定的项(众所周知,SelectedItems不是可绑定的属性)。我实际上正在使用Telerik RadGridView,但我希望该行为对于带有SelectionChanged事件的任何事物都是通用的。但是,不同的控件对SelectionChanged事件处理程序具有不同的签名(RadGridView使用Telerik.Windows.Controls.SelectionChangeEventArgs,而标准GridView使用System.Windows.Controls.SelectionChangedEventArgs)。我们可以确定的一件事是,事件args将从EventArgs派生(实际上,我们可以确定它将从RoutedEventArgs派生)。
但是,虽然我可以编写一个将RoutedEventArgs作为其第二个参数的常规事件处理程序,并且可以使用反射来获取SelectionChangedEvent的EventInfo,但如果不使用事件的精确签名,就无法将处理程序挂接到事件上处理程序-在这种情况下为RadGridView处理程序。
这是我的代码。我已经包含了所有内容,但重要的是SelectItemPropertyChanged,它是DependencyObject PropertyChangedCallback,它试图将事件处理程序SelectionChangedHandler连接到SelectionChangedEvent。 (SelectionChangedHandler中的代码与该问题无关,但我将其保留在其中,这样很清楚我在做什么)。
public static class SelectedItemsChangedBehaviour{
public static readonly DependencyProperty SelectItemsProperty =
DependencyProperty.RegisterAttached("SelectItems", typeof(bool), typeof(SelectedItemsChangedBehaviour),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(SelectItemPropertyChanged)));
public static void SetSelectItems(DependencyObject dependencyObject, bool selectItems)
{
dependencyObject.SetValue(SelectItemsProperty, selectItems);
}
public static bool GetSelectItems(DependencyObject dependencyObject)
{
return (bool)dependencyObject.GetValue(SelectItemsProperty);
}
private static void SelectItemPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
// No common base for all classes with SelectionChanged event so use reflection
EventInfo selectionChangedEventInfo = dependencyObject.GetType().GetEvent("SelectionChanged");
if (selectionChangedEventInfo == null)
{
throw new ArgumentException("Must have a SelectionChanged event.");
}
if ((bool)dependencyPropertyChangedEventArgs.OldValue)
{
// This is what I want to do, but it throws because the handler signature is wrong
selectionChangedEventInfo.RemoveEventHandler(dependencyObject, (RoutedEventHandler)SelectionChangedHandler);
// This works fine because it is of the right type for the RadGridView but is not general
//selectionChangedEventInfo.RemoveEventHandler(dependencyObject, (EventHandler<Telerik.Windows.Controls.SelectionChangeEventArgs>)SelectionChangedHandler);
}
if ((bool)dependencyPropertyChangedEventArgs.NewValue)
{
// This is what I want to do, but it throws because the handler signature is wrong
selectionChangedEventInfo.AddEventHandler(dependencyObject, (RoutedEventHandler)SelectionChangedHandler);
// This works fine because it is of the right type for the RadGridView but is not general
//selectionChangedEventInfo.AddEventHandler(dependencyObject, (EventHandler<Telerik.Windows.Controls.SelectionChangeEventArgs>)SelectionChangedHandler);
}
}
private static void SelectionChangedHandler(object sender, RoutedEventArgs eventArgs)
{
// No common base for all classes with AddedItems/RemovedItems (eg. System.Windows.Controls.SelectionChangedEventArgs / Telerik.Windows.Controls.SelectionChangeEventArgs
PropertyInfo addedItemsInfo = eventArgs.GetType().GetProperty("AddedItems");
PropertyInfo removedItemsInfo = eventArgs.GetType().GetProperty("RemovedItems");
if (addedItemsInfo == null || removedItemsInfo == null)
{
throw new ArgumentException("Must have AddedItems and RemovedItems");
}
foreach (object item in (IEnumerable)addedItemsInfo.GetValue(eventArgs, null))
{
((ISelectable)item).IsSelected = true;
}
foreach (object item in (IEnumerable)removedItemsInfo.GetValue(eventArgs, null))
{
((ISelectable)item).IsSelected = false;
}
}
我尝试了各种方法来使用反射为处理程序获取正确的签名,从而为正确的类型创建一个委托,但我无法使其正常工作-AddEventHandler(和RemoveEventHandler)抛出InvalidArgumentException,完整的堆栈跟踪如下:
{“类型'System.Windows.RoutedEventHandler'的对象不能转换为类型'System.EventHandler`1 [Telerik.Windows.Controls.SelectionChangeEventArgs]'。”}
在System.RuntimeType.TryChangeType处(对象值,活页夹活页夹,CultureInfo文化,布尔值NeedSpecialCast)
有人可以建议吗?
最佳答案
调用EventHandlerType
时,需要将委托转换为事件的AddEventHandler
。这是一个示例应用程序:
using System;
using System.Reflection;
using System.Threading;
namespace App
{
class Program
{
public event EventHandler<ThreadExceptionEventArgs> E;
static void Main ()
{
new Program().Run();
}
private void Run ()
{
EventInfo e = typeof(Program).GetEvent("E");
EventHandler untypedHandler = OnE;
Delegate typedHandler = Delegate.CreateDelegate(e.EventHandlerType,
untypedHandler.Target, untypedHandler.Method);
e.AddEventHandler(this, typedHandler);
E(this, new ThreadExceptionEventArgs(new Exception("Hello world!")));
}
private void OnE (object sender, EventArgs args)
{
Console.WriteLine(((ThreadExceptionEventArgs)args).Exception.Message);
}
}
}
关于c# - 如何将事件处理程序委托(delegate)转换为具有不同签名的委托(delegate),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25786964/