Jim Lynn

Stuff I’ve learned. Stuff I think.

‘Cannot register duplicate name ‘XXX’ in this scope’ in VS 2010

Posted by jimlynn on February 12, 2010

Here’s a gotcha that was puzzling me yesterday.

I’ve just installed Visual Studio 2010 RC, and was trying it out on my current project. It’s a Silverlight Navigation-style project, but that’s not important to the bug.

I found one page where the Xaml designer wouldn’t handle the page properly – it was throwing exceptions, and the editor was showing an error in the Xaml. The line looked like this:


<local:SimpleConverter x:Name="SimpleConverter"/>

This is a value converter, designed to convert bindings from one type to another. The error it was showing was ‘Cannot register duplicate name ‘SimpleConverter’ in this scope’. This foxed me for a while – I thought perhaps because I was throwing exceptions when I didn’t recognise the type being converted, but even removing that and simplifying didn’t remove the error.

Then I noticed the key word in the error message: ‘Name’.

In Xaml you can use x:Name if you want something in the Xaml linked up to a class variable in your code-behind. But that was clearly causing issues with whatever the designer was doing behind the scenes. However, if you don’t need code-behind access (as I don’t in this case) you can use x:Key – and that’s the usual mechanism for naming resources.

Changing the resource to:


<local:SimpleConverter x:Key="SimpleConverter"/>

then the errors from the visual designer stop happening.

Of course, I’ve no idea if the errors are a bug in the designer, or if it’s just wrong to use x:Name in resources, but since I didn’t need the autowiring up of objects, it’s no problem to change it.

22 Responses to “‘Cannot register duplicate name ‘XXX’ in this scope’ in VS 2010”

  1. Larry said

    MAD PROPS UNTO THEE!!!

    I had the same issue and it was driving me nuts!!!

  2. Rochelle said

    Thanks for the help!
    Saved me a lot of time!

  3. Joe said

    yeah, thanks. was driving me nuts as well.

  4. ACanadian said

    Thank for the post… this helped me out!!

  5. Roy Dallal said

    Thank you,
    I tried renaming but it just didn’t work…

  6. sanunu said

    十分感谢,非常有帮助。

  7. Seriously, this has been driving me batty for years. Where do I sent the check?

  8. Wes said

    Thanks!!!

  9. Radek said

    To define Resource, use x:Key, not x:Name
    x:Key=”TestDC”

  10. Dom said

    Keep this coming to the top of Google, saved me some head scratching, thanks for the post

  11. Thanks you for the post!

  12. Thanks! saved me time!

  13. Kaps said

    That was awesome find. Great :)

  14. Barry said

    It should be so obvious… but it isn’t. I’m glad this was number one on my search, it could have eaten a lot of time for something so stupid. Thanks!

  15. mike said

    Thank you!

  16. Jiten Oswal said

    Awesome man. Thanks I was trying to solve this issue since last week and I was going crazy. :(
    Thank you very muchh .. :D
    Respect \m/

  17. Lars said

    Kudos! Saved my day :-)

  18. zeeshan ajmal said

    thanx a billion man, save my time !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: