2012-01-20 83 views
1

當我嘗試在XAML中註冊事件處理程序時,出現XamlParseException。RoutedPropertyChangedEventHandler自定義UserControl - 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添加到可視化樹時會引發此異常。

爲什麼我無法註冊我的活動?

回答

2

如果你實際註冊了正確的名字和類型的事件,這應該工作得很好。

ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged", ..., typeof(RoutedPropertyChangedEventHandler<double>), typeof(NumericUpDown)); 
+0

我以爲RegisterRoutedEvent不相關,但它是。我有'typeof(RoutedPropertyChangedEventArgs )'。 :(|) –

+1

我也這麼做了,也是一段時間。 –

+0

哦,很好。畢竟我並不笨。 :) –

相關問題