I copied three classes in from another WPF project and then changed their namespaces.
Now when I run the project, I get errors in the \".g.cs\"
The .g.cs file is generated from the .xaml file. You need to change the x:class= attribute in your .xaml file to match the new namespace-qualified class name; then the .g.cs will be generated correctly on next compile. (Don't manually change the .g.cs file -- you'll only frustrate yourself.)
For example, if you previously had this in your .cs:
namespace Foo {
class Bar { ...
and this in your .xaml:
<UserControl x:Class="Foo.Bar" ...
And then you changed your namespace:
namespace Baz {
class Bar { ...
Then you would need to change your .xaml file to:
<UserControl x:Class="Baz.Bar" ...
I had defined XML namespace shortcuts in my AssemblyInfo.cs
[assembly: XmlnsDefinition("http://www.example.com/wpf", "MyCorp.Wpf.OldNamespace")]
where the old namespace was still present. Searching through all my *.xaml files obviously did not help …
After fixing the shortcuts, the error went away immediately.
I had this issue and tried deleting the obj
and bin
folders, but that would not work. It finally compiled when I selected Project > Clean
and then Project > Build
. There must be some Visual Studio cache somewhere else on my system.
Another possible cause is if you have any xmlns:xx
namespaces in your xaml which you neglected to update when changing a namespace, then this will also cause an invalid using statement in the g.cs file.
Did you update the namespace of the class in the x:Class
attribute on UserControl
in the XAML too?
Failing that, have you tried rebuilding the project?
I found another way to solve this by deleting your 'obj' folder and rebuilding.