当我尝试在XAML中注册事件处理程序时,我收到了XamlParseException。
我的活动:
public partial class NumericUpDown : UserControl
{
public static readonly RoutedEvent ValueChangedEvent;
public event RoutedPropertyChangedEventHandler<double> ValueChanged
{
add
{
AddHandler(ValueChangedEvent, value);
}
remove
{
RemoveHandler(ValueChangedEvent, value);
}
}
}
我的XAML注册活动:
<local:NumericUpDown x:Key="Numeric" ValueChanged="NumericUpDown_ValueChanged" />
我的事件处理程序:
private void NumericUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
}
例外:
System.Windows.Markup.XamlParseException occurred
Message='Set property 'EbmsPickerTicket.NumericUpDown.ValueChanged' threw an exception.' Line number '153' and line position '6'.
Source=PresentationFramework
LineNumber=153
LinePosition=6
StackTrace:
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadDeferredContent(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings parentSettings, Uri baseUri)
at System.Windows.ResourceDictionary.CreateObject(KeyRecord key)
at System.Windows.ResourceDictionary.RealizeDeferContent(Object key, Object& value, Boolean& canCache)
at System.Windows.ResourceDictionary.GetValueWithoutLock(Object key, Boolean& canCache)
at System.Windows.ResourceDictionary.GetValue(Object key, Boolean& canCache)
at System.Windows.ResourceDictionary.get_Item(Object key)
at EbmsPickerTicket.MainWindow.Window_GotFocus(Object sender, RoutedEventArgs e) in C:\Users\Kendall\Documents\Visual Studio 2010\Projects\EbmsPickerTicket\EbmsPickerTicket\MainWindow.xaml.cs:line 71
InnerException: System.ArgumentException
Message=Object of type 'System.String' cannot be converted to type 'System.Windows.RoutedPropertyChangedEventHandler`1[System.Double]'.
Source=mscorlib
StackTrace:
at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Xaml.Schema.XamlMemberInvoker.SetValueSafeCritical(Object instance, Object value)
at System.Xaml.Schema.XamlMemberInvoker.SetValue(Object instance, Object value)
at System.Windows.Baml2006.WpfMemberInvoker.SetValue(Object instance, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
InnerException:
请注意内部异常消息。
将自定义UserControl添加到可视树时会抛出此异常。
为什么我不能注册我的活动?
答案 0 :(得分:2)
如果您使用正确的名称和类型实际注册了该事件,那么这应该可以正常工作。
ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged", ..., typeof(RoutedPropertyChangedEventHandler<double>), typeof(NumericUpDown));