RoutedEvent 名称已使用

发布于 2024-10-17 20:16:54 字数 3924 浏览 7 评论 0原文

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

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

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

AccountConfigurationView:

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
}

SystemConfigurationView:

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
}

UserConfigurationView:

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 消息:

已使用 OwnerType“baskcode.Admin.Controls.AccountConfigurationView”的 RoutedEvent 名称“ConfigurationSaved”。

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

我正在使用 .Net 版本 4

谢谢。

In WPF, I've created 3 UserControls "User Configuration", "System Configuration" & "Account Configuration". All these user controls have "Save" & "Cancel" buttons. On click of these buttons they raise a Routed Event declared and defined in their respective classes. On clicking the Save button "ConfigurationSaved" event is raised & on Cancel button "ConfigurationCancelled" event is raised.

These events when raised, the container which hosts the user control will take care of saving the configuration.

Code Snippet for all the class's routed event definition is as follows:

AccountConfigurationView:

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
}

SystemConfigurationView:

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
}

UserConfigurationView:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染柒℉ 2024-10-24 20:16:54

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

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));
}

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));
}
热情消退 2024-10-24 20:16:54

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

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

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

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

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

using System.Linq;

...

RoutedEvent[] x = EventManager.GetRoutedEvents();

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

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)
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文