开发者

Remove an old namespace from a g.cs File?

I previously had a subfolder in my WPF application project called "Controls". It contained a WPF user control. I decided to m开发者_StackOverflowove that user control to my "Views" folder. Since the original folder was empty, I decided to delete it from the project.

Because the user control and folder is removed I receive a compilation error because the user control used the ProjectName.Folder namespace and now nothing references it. MainWindow.g.cs is what references ProjectName.Controls in a using statement.

I know that *.g.cs are generated by VS and can't be edited because it will be overwritten. What do I do to not allow that namespace to be written to the g.cs file? I tried cleaning my solution/project and rebuilding but nothing has worked.


I had a local reference to the Controls namespace in my Xaml code (MainWindow.xaml). I removed the reference, cleaned the project and produced a successful build.


In your user control file,

In your ClassName.xaml, you must change the namespace as shown below

<UserControl
    x:Class="YourOldNamespace.ClassName"
    ...
    ...
/>

And in your ClassName.xaml.cs, you must change the namespace as shown below

using System;
using System.Windows;

namespace YourOldNamespace{

   public class ClassName{ ....

}

In both the files, you must replace YourOldNamespace to some new namespace as needed.


I have had problems with g.cs files in my projects before too. Since they are auto generated, I tend to just delete the file manually and rebuild.


Dont forget too, that you must check to see if the Build Action property when you click on the affected XAML file is set to PAGE (instead of resource). This is useful to know when you copy a XAML from another project using copy-paste to save time.


Also look at App.xaml and all of your resource dictionaries. For whatever reason, VS 2012's replace in files / "Entire Solution" option didn't find the old namespace reference in App.xaml, had to manually change that. Fixed it for me.


Don't forget to change your Generic.xaml file too,

<ResourceDictionary 
                   xmlns:local="clr-namespace:MyOldNameSpace">
</ResourceDictionary>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜