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.
MAD PROPS UNTO THEE!!!
I had the same issue and it was driving me nuts!!!
Sorry,Could you detail it?coz i dont very understand
Thanks for the help!
Saved me a lot of time!
yeah, thanks. was driving me nuts as well.
Thank for the post… this helped me out!!
Thank you,
I tried renaming but it just didn’t work…
十分感谢,非常有帮助。
不客气。
(I hope the translator worked!)
Seriously, this has been driving me batty for years. Where do I sent the check?
No charge. My pleasure.
Thanks!!!
To define Resource, use x:Key, not x:Name
x:Key=”TestDC”
Thanks!!!
Keep this coming to the top of Google, saved me some head scratching, thanks for the post
Thanks you for the post!
Thanks! saved me time!
That was awesome find. Great 🙂
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!
Thank you!
Awesome man. Thanks I was trying to solve this issue since last week and I was going crazy. 😦
Thank you very muchh .. 😀
Respect \m/
Kudos! Saved my day 🙂
thanx a billion man, save my time !
This has officially made my day. Thanks! 🙂