1

在 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
}

When I'm using these classes I'm getting TypeInitializationException with the message:

RoutedEvent Name 'ConfigurationSaved' for OwnerType 'baskcode.Admin.Controls.AccountConfigurationView' already used.

Same exception is thrown if I try to load any of the other controls. I'm not able to rectify the problem. Please help me in this regard.

I'm using .Net version 4

Thanks.

4

2 回答 2

4

Check your EventManager.RegisterRoutedEvent arguments. If you've copied and pasted it from another file it's easy to forget to change the owner type. For example:

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

But then you copy-paste that to another file. Notice the mistaken typeof(UserControlA) below, which should be typeof(UserControlB).

public class UserControlB
{
  public static readonly RoutedEvent ClickEvent =
    EventManager.RegisterRoutedEvent(
      "Click",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(UserControlA));
}
于 2013-02-17T17:48:21.103 回答
1

I assume that you are initializing your control more than once. You can't register the same routed event name more than once.

Try this (for one RoutedEvent):

RoutedEvent[] x = EventManager.GetRoutedEventsForOwner(typeof(UserConfigurationView));

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

And this (for multiple instanaces) : in your case

using System.Linq;

...

RoutedEvent[] x = EventManager.GetRoutedEvents();

if(!x.Contains(ConfigurationSaved)) {
    ConfigurationSavedEvent = EventManager.RegisterRoutedEvent(
        "ConfigurationSaved",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler), 
        typeof(UserConfigurationView)
    );
}
于 2011-05-21T16:38:21.997 回答