简体   繁体   English

Visual Studio 2013 WPF UserControl RoutedEvent名称已使用?

[英]Visual Studio 2013 WPF UserControl RoutedEvent Name Already Used?

I have a WPF project in Visual Studio 2013 which contains a custom control I created, containing a custom event. 我在Visual Studio 2013中有一个WPF项目,其中包含我创建的自定义控件,其中包含一个自定义事件。 Whenever I drag the control into my MainWindow.xaml file, it will display once in the designer. 每当我将控件拖到MainWindow.xaml文件中时,它将在设计器中显示一次。 As soon as I compile my application (which succeeds), the designer will display something like this, showing my control is broken: 一旦我编译了应用程序(成功),设计器就会显示如下内容,表明我的控件已损坏:

在此处输入图片说明

In the XAML code (again, only after the first compile), I get this tip: 在XAML代码中(同样,只有在第一次编译之后),我才得到以下提示:

在此处输入图片说明

However, no matter what I change my event name to (say "EggsAndBaconEvent"), it will always break the designer after the first compile. 但是,无论我将事件名称更改为什么(例如“ EggsAndBaconEvent”),在第一次编译后,它总是会破坏设计器。

Visual Studio will try to help me in this window: Visual Studio将在此窗口中尝试帮助我:

在此处输入图片说明

Here is my code which is related to creating an event: 这是与创建事件有关的代码:

public partial class Settings : UserControl {
    ...

    public static RoutedEvent ExpandEvent;

    public Settings() {
        ...

        ExpandEvent = EventManager.RegisterRoutedEvent("Expanded", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Settings));
    }

    ...

    public event RoutedEventHandler Exapnd {
        add { AddHandler(ExpandEvent, value); }
        remove { RemoveHandler(ExpandEvent, value); }
    }

    private void expanderChanged(object sender, RoutedEventArgs e) {
        ...

        RoutedEventArgs args = new RoutedEventArgs(ExpandEvent);
        RaiseEvent(args);
    }
}

Does anyone know why Visual Studio is complaining? 有谁知道为什么Visual Studio在抱怨? Is it my code or is this some strange anomaly in VS 2013? 是我的代码还是这是VS 2013中的一些奇怪异常?

Thank you for your time. 感谢您的时间。

After more researching, I found that it was Design Mode trying to execute code it couldn't evaluate properly. 经过更多的研究,我发现这是设计模式试图执行无法正确评估的代码。 To prevent Design Mode from doing this, wrap the offending code, like this: 为了防止“设计模式”执行此操作,请包装令人讨厌的代码,如下所示:

if(!DesignerProperties.GetIsInDesignMode(this)) {
    ... Design Mode won't look at this ...
}

So, in my case, if I wrap the event registration, like this: 因此,就我而言,如果包装事件注册,如下所示:

if(!DesignerProperties.GetIsInDesignMode(this)) {
    ExpandEvent = EventManager.RegisterRoutedEvent(
        "Expanded",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler),
        typeof(Settings)
    );
}

... Design Mode will not break, and the application continues to run as expected. ...设计模式不会中断,并且应用程序将继续按预期运行。

Here are some cases where I found you may need to include such a preventative: 在某些情况下,我发现您可能需要包含此类预防措施:

  • Event Registration 活动注册
  • Accessing app.config 访问app.config
  • Using C# to update any control properties at initialization time 在初始化时使用C#更新任何控件属性

That error means exactly what it says: 该错误确切说明了它的意思:

There is a RoutedEvent named Expanded that has already been registered with an OwnerType of Settings 有一个名为ExpandedRoutedEvent ,已通过OwnerType of Settings注册。

That means that you have somehow duplicated this line of code: 这意味着您已经以某种方式重复了以下代码行:

ExpandEvent = EventManager.RegisterRoutedEvent(
    "Expanded",                    /* name */
    RoutingStrategy.Bubble,        /* routingStrategy */ 
    typeof(RoutedEventHandler),    /* handlerType */
    typeof(Settings)               /* ownerType <--- Duplicated */
);

You may have copied and pasted it into a different control, but forgot to update the OwnerType value from Settings to the correct class name. 可能已将其复制并粘贴到其他控件中,但是忘记将OwnerType值从Settings更新为正确的类名。 If you search for typeof(Settings) , you should find it. 如果搜索typeof(Settings) ,则应该找到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF EventTriggers-在Visual Studio 2008中无法将属性&#39;RoutedEvent&#39;中的字符串&#39;DragOver&#39;转换为类型&#39;System.Windows.RoutedEvent&#39;的对象 - WPF EventTriggers - Cannot convert string 'DragOver' in attribute 'RoutedEvent' to object of type 'System.Windows.RoutedEvent' in Visual Studio 2008 Visual Studio 2013-WPF属性会定期冻结 - Visual Studio 2013 - WPF properties freeze periodically 找不到UserControl中的自定义RoutedEvent - Custom RoutedEvent in UserControl not found 如何在Visual Studio 2013中命名内容页面 - How to Name Content Pages in Visual Studio 2013 WPF派生的UserControl不会在Visual Studio中打开(但会运行) - WPF Derived UserControl Doesn't Open in Visual Studio (but runs) 更改新 Window 和 UserControl (WPF) 的默认 Visual Studio 模板 - Change default Visual Studio template for new Window and UserControl (WPF) 需要 Visual Studio 2019 Extension ToolWindow ( WPF UserControl ) 的特定大小 - Desire specific size of Visual Studio 2019 Extension ToolWindow ( WPF UserControl ) 在使用Visual Studio的WPF应用程序中,如何将用户控件绑定到用户控件变量? - In a WPF application using Visual Studio, how can I bind a usercontrol to a usercontrol variable? Visual Studio 2013 Excel VSTO将WPF或Windows窗体添加到文档 - Visual Studio 2013 Excel VSTO Add WPF Or Windows Form To Document Visual Studio 2013 Express WPF设计器加载失败 - Visual Studio 2013 Express WPF Deigner load failure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM