After changing namespace in UserControl files, …g.cs files get errors

前端 未结 6 1258
陌清茗
陌清茗 2021-02-18 18:54

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\"

相关标签:
6条回答
  • 2021-02-18 19:04

    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" ...
    
    0 讨论(0)
  • 2021-02-18 19:15

    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.

    0 讨论(0)
  • 2021-02-18 19:18

    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.

    0 讨论(0)
  • 2021-02-18 19:22

    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.

    0 讨论(0)
  • 2021-02-18 19:23

    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?

    0 讨论(0)
  • I found another way to solve this by deleting your 'obj' folder and rebuilding.

    0 讨论(0)
提交回复
热议问题