[英]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: 在某些情况下,我发现您可能需要包含此类预防措施:
That error means exactly what it says: 该错误确切说明了它的意思:
There is a
RoutedEvent
namedExpanded
that has already been registered with anOwnerType
ofSettings
有一个名为Expanded
的RoutedEvent
,已通过OwnerType
ofSettings
注册。
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.