Wpf RouteEvent名称已被使用

Wpf RouteEvent名称已被使用,wpf,user-controls,routedevent,Wpf,User Controls,Routedevent,在WPF中,我创建了3个用户控件“用户配置”、“系统配置”和“帐户配置”。所有这些用户控件都有“保存”和“取消”按钮。单击这些按钮,它们将引发在各自的类中声明和定义的路由事件。单击保存按钮时引发“ConfigurationSaved”事件,单击取消按钮时引发“ConfigurationCancelled”事件 引发这些事件时,承载用户控件的容器将负责保存配置 所有类的路由事件定义的代码段如下所示: 帐户配置视图: public partial class AccountConfiguration

在WPF中,我创建了3个用户控件“用户配置”、“系统配置”和“帐户配置”。所有这些用户控件都有“保存”和“取消”按钮。单击这些按钮,它们将引发在各自的类中声明和定义的路由事件。单击保存按钮时引发“ConfigurationSaved”事件,单击取消按钮时引发“ConfigurationCancelled”事件

引发这些事件时,承载用户控件的容器将负责保存配置

所有类的路由事件定义的代码段如下所示:

帐户配置视图:

public partial class AccountConfigurationView : UserControl
{
    static AccountConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}
public partial class SystemConfigurationView : UserControl
{
    static SystemConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}
public partial class UserConfigurationView : UserControl
{
    static UserConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}
系统配置视图:

public partial class AccountConfigurationView : UserControl
{
    static AccountConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}
public partial class SystemConfigurationView : UserControl
{
    static SystemConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}
public partial class UserConfigurationView : UserControl
{
    static UserConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}
用户配置视图:

public partial class AccountConfigurationView : UserControl
{
    static AccountConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}
public partial class SystemConfigurationView : UserControl
{
    static SystemConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}
public partial class UserConfigurationView : UserControl
{
    static UserConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}
当我使用这些类时,我会收到TypeInitializationException,并显示以下消息:

已使用所有者类型“baskcode.Admin.Controls.AccountConfigurationView”的RoutedEvent名称“ConfigurationSaved”

如果我尝试加载任何其他控件,则会引发相同的异常。我无法纠正这个问题。请在这方面帮助我

我正在使用.Net版本4


谢谢。

我假设您多次初始化控件。不能多次注册同一路由事件名称

试试这个(对于一个
RoutedEvent
):

这(对于多个实例):在您的情况下

using System.Linq;

...

RoutedEvent[] x = EventManager.GetRoutedEvents();

if(!x.Contains(ConfigurationSaved)) {
    ConfigurationSavedEvent = EventManager.RegisterRoutedEvent(
        "ConfigurationSaved",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler), 
        typeof(UserConfigurationView)
    );
}

我假设您多次初始化控件。不能多次注册同一路由事件名称

试试这个(对于一个
RoutedEvent
):

这(对于多个实例):在您的情况下

using System.Linq;

...

RoutedEvent[] x = EventManager.GetRoutedEvents();

if(!x.Contains(ConfigurationSaved)) {
    ConfigurationSavedEvent = EventManager.RegisterRoutedEvent(
        "ConfigurationSaved",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler), 
        typeof(UserConfigurationView)
    );
}

检查EventManager.RegisterRoutedEvent参数。如果您从另一个文件复制并粘贴了它,很容易忘记更改所有者类型。例如:

public class UserControlA
{
  public static readonly RoutedEvent ClickEvent =
    EventManager.RegisterRoutedEvent(
      "Click",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(UserControlA));
}
但是你可以复制粘贴到另一个文件。注意下面错误的typeof(UserControlA),应该是typeof(UserControlB)

public class UserControlB
{
  public static readonly RoutedEvent ClickEvent =
    EventManager.RegisterRoutedEvent(
      "Click",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(UserControlA));
}

检查EventManager.RegisterRoutedEvent参数。如果您从另一个文件复制并粘贴了它,很容易忘记更改所有者类型。例如:

public class UserControlA
{
  public static readonly RoutedEvent ClickEvent =
    EventManager.RegisterRoutedEvent(
      "Click",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(UserControlA));
}
但是你可以复制粘贴到另一个文件。注意下面错误的typeof(UserControlA),应该是typeof(UserControlB)

public class UserControlB
{
  public static readonly RoutedEvent ClickEvent =
    EventManager.RegisterRoutedEvent(
      "Click",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(UserControlA));
}