1

I have a WPF project in Visual Studio 2013 which contains a custom control I created, containing a custom event. Whenever I drag the control into my MainWindow.xaml file, it will display once in the designer. As soon as I compile my application (which succeeds), the designer will display something like this, showing my control is broken:

enter image description here

In the XAML code (again, only after the first compile), I get this tip:

enter image description here

However, no matter what I change my event name to (say "EggsAndBaconEvent"), it will always break the designer after the first compile.

Visual Studio will try to help me in this window:

enter image description here

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? Is it my code or is this some strange anomaly in VS 2013?

Thank you for your time.

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195

2 Answers2

1

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
  • Using C# to update any control properties at initialization time
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • In my case this doesn't help. Only explicit unsubscribe from the event helps (http://stackoverflow.com/a/13930385/585819). But for this I need to run and close the application. – alehro May 12 '17 at 09:35
0

That error means exactly what it says:

There is a RoutedEvent named Expanded that has already been registered with an 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. If you search for typeof(Settings), you should find it.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I did a Solution-wide search for `typeof(Settings)` and did not find it anywhere else. – Oliver Spryn Jun 05 '14 at 12:10
  • Then try a Clean and then Build operation. There is no other reason for this error and I have never seen it occur incorrectly. – Sheridan Jun 05 '14 at 12:13
  • So, cleaning works, until the first compile, then it continues to reiterate the same error. That is why I'm so stumped. :) – Oliver Spryn Jun 05 '14 at 12:15
  • Ok, try this: Comment out your `ExpandEvent` declaration and Rebuild the project. The error should disappear. Then uncomment it and Rebuild. – Sheridan Jun 05 '14 at 12:16
  • Commenting out the event entirely causes everything to work well. However, if I just try the declaration, I can't compile, since it says the `ExpandEvent` is not defined... :( – Oliver Spryn Jun 05 '14 at 12:23
  • Well clearly, if something references the event, you'd need to comment that out as well. So after commenting it all out and then uncommenting it, you still get the error? If you do, I'd say that it's time for you to restart Visual Studio. – Sheridan Jun 05 '14 at 12:33
  • No, nothing currently references the event, and I had commented out all of the event declarations and definitions. That worked, until I added all of the definitions back in. Restarting VS wasn't any good, either, as this has been a problem since yesterday. – Oliver Spryn Jun 05 '14 at 12:50
  • Sorry then, I'm all out of suggestions. – Sheridan Jun 05 '14 at 12:57