开发者

Getting a XamlParse Exception when using a userbaseclass with a UserControl

I have an abstract base class that subclasses the UserControl class with no XAML. When I create a class based on the base class ever开发者_StackOverflowything works fine (compiles and executes). But when I add code to the base class to fire an event, it compiles but when run I get a 'The invocation of the constructor on type 'ExtendedDisplay.UserControls.Annotations' that matches the specified binding constraints threw an exception.' error. Not sure why. Here is my code for the base class,

public abstract class BaseClass: UserControl { protected static System.Type ControlType;

  public static readonly RoutedEvent RefreshEvent = EventManager.RegisterRoutedEvent(
       "RefreshEvent",
       RoutingStrategy.Bubble,
       typeof(RefreshEventHandler),
       ControlType);

  public delegate void RefreshEventHandler(object sender, RefreshEventArgs e);

  public event RefreshEventHandler RefreshNeeded
  {
      add { AddHandler(RefreshEvent, value); }
      remove { RemoveHandler(RefreshEvent, value); }
  }

  protected void RaiseRefreshEvent(RoutedEventArgs e)
  {
      RaiseEvent(new RefreshEventArgs(RefreshEvent, this));

      e.Handled = true;
  }

  public class RefreshEventArgs : RoutedEventArgs
  {
      public RefreshEventArgs(RoutedEvent routedEvent, object source)
            : base(routedEvent, source) { }
  }

}

This code works as expected when hardcoded into the UserControl. Any ideas/help would be greatly appreciated.

On further testing it appears that I can't replace the last parameter in the EventManager.RegisterRoutedEvent function with a variable. I have a variable "protected static System.Type ControlType;" that is set to the typeof control by the derived control. This doesn't work when hard coded either. To make it work, I can't use a variable, even though it is the right type. Is ther any way around this?


Ok, Finally figured it out. What I did was to have the baseclass member "RefreshEvent" just declared in the baseclass and assigned in the derived class. This works great and does what I need keeping the bulk of the boilerplate in the baseclass.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜