‘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.
Larry said
MAD PROPS UNTO THEE!!!
I had the same issue and it was driving me nuts!!!
eddy said
Sorry,Could you detail it?coz i dont very understand
Rochelle said
Thanks for the help!
Saved me a lot of time!
Joe said
yeah, thanks. was driving me nuts as well.
ACanadian said
Thank for the post… this helped me out!!
Roy Dallal said
Thank you,
I tried renaming but it just didn’t work…
sanunu said
十分感谢,非常有帮助。
jimlynn said
不客气。
(I hope the translator worked!)
Shaun McDonnell said
Seriously, this has been driving me batty for years. Where do I sent the check?
jimlynn said
No charge. My pleasure.
Wes said
Thanks!!!
Radek said
To define Resource, use x:Key, not x:Name
x:Key=”TestDC”
Max said
Thanks!!!
Dom said
Keep this coming to the top of Google, saved me some head scratching, thanks for the post
Daniel Ionescu said
Thanks you for the post!
Al (@Tsikavy) said
Thanks! saved me time!
Kaps said
That was awesome find. Great
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!
mike said
Thank you!
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 ..
Respect \m/
Lars said
Kudos! Saved my day
zeeshan ajmal said
thanx a billion man, save my time !