我在XAML中创建了一个旋钮用户控件,并希望它在值更改时引发自定义事件。我基于Microsoft Docs Example为基础,并提出了以下代码:
KnobControl.xaml.cs:
public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(KnobControl));
//Events
public event RoutedEventHandler ValueChanged
{
add { AddHandler(ValueChangedEvent, value); }
remove { RemoveHandler(ValueChangedEvent, value); }
}
ChannelStrip.xaml(其中包含控件的xaml):
<KnobControlNamespace:KnobControl x:Name="ChannelPan" arcEndAngle="145" arcStartAngle="-145" Title="Pan" Unit="" Maximum="50" LabelFontSize="34" Step="1" Power="1" Foreground="{DynamicResource Text}" Grid.Row="2" Margin="0,2" Minimum="-50" ValueChanged="ChannelPan_ValueChanged"/>
最后是ChannelStrip.xaml.cs(这是事件处理程序所在的位置):
public void ChannelPan_ValueChanged(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs(PanEvent));
}
运行此命令时,我得到:'Failed to create a 'ValueChanged' from the text 'ChannelPan_ValueChanged'
以及ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.
的内部异常
我花了一段时间检查示例,但由于我对使用事件和WPF还是很陌生,因此我无法找到足够的错误来自己调试一下。我感到奇怪的是,由于在实现此自定义事件和事件处理程序之前,我仅在此特定行出现错误,其他事件处理程序运行良好,并且它们的方法签名相同(当然不是名称)。
我应该如何尝试调试它?