【问题标题】:Getting a XamlParse Exception when using a userbaseclass with a UserControl将用户基类与 UserControl 一起使用时出现 XamlParse 异常
【发布时间】:2011-04-28 15:18:21
【问题描述】:

我有一个抽象基类,它继承了没有 XAML 的 UserControl 类。当我基于基类创建一个类时,一切正常(编译和执行)。但是,当我将代码添加到基类以触发事件时,它会编译,但在运行时我得到一个“与指定的绑定约束匹配的'ExtendedDisplay.UserControls.Annotations' 类型的构造函数的调用引发了异常。”错误。不知道为什么。 这是我的基类代码,

公共抽象类BaseClass:UserControl { 受保护的静态 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) { }
  }

}

当硬编码到 UserControl 中时,此代码按预期工作。任何想法/帮助将不胜感激。

在进一步测试中,我似乎无法用变量替换 EventManager.RegisterRoutedEvent 函数中的最后一个参数。我有一个变量“受保护的静态 System.Type ControlType;”由派生控件设置为 typeof 控件。这在硬编码时也不起作用。为了使它工作,我不能使用变量,即使它是正确的类型。有没有办法解决这个问题?

【问题讨论】:

    标签: c# wpf wpf-controls


    【解决方案1】:

    好的, 终于想通了。我所做的是让基类成员“RefreshEvent”刚刚在基类中声明并在派生类中分配。这很好用,并且可以满足我将大部分样板保留在基类中的需要。

    【讨论】:

      猜你喜欢
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多