首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >已使用的RoutedEvent名称

已使用的RoutedEvent名称
EN

Stack Overflow用户
提问于 2011-02-17 11:37:53
回答 2查看 2.6K关注 0票数 1

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

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

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

AccountConfigurationView:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-21 16:38:21

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

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

代码语言:javascript
复制
RoutedEvent[] x = EventManager.GetRoutedEventsForOwner(typeof(UserConfigurationView));

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

这个(用于多个实例化):在您的例子中

代码语言:javascript
复制
using System.Linq;

...

RoutedEvent[] x = EventManager.GetRoutedEvents();

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

Stack Overflow用户

发布于 2013-02-17 17:48:21

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

代码语言:javascript
复制
public class UserControlA
{
  public static readonly RoutedEvent ClickEvent =
    EventManager.RegisterRoutedEvent(
      "Click",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(UserControlA));
}

但是你把它复制粘贴到另一个文件上。注意下面错误的typeof(UserControlA),应该是typeof(UserControlB).

代码语言:javascript
复制
public class UserControlB
{
  public static readonly RoutedEvent ClickEvent =
    EventManager.RegisterRoutedEvent(
      "Click",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(UserControlA));
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5028419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档