更改 UserControl 文件中的命名空间后,...g.cs 文件出错 [英] After changing namespace in UserControl files, ...g.cs files get errors

查看:33
本文介绍了更改 UserControl 文件中的命名空间后,...g.cs 文件出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从另一个 WPF 项目中复制了三个类,然后更改了它们的命名空间.

I copied three classes in from another WPF project and then changed their namespaces.

现在,当我运行该项目时,我在.g.cs"文件中遇到错误,表明命名空间不正确.我更改了这个命名空间,但是当我再次编译并运行时,.g.cs"文件被重新生成并被旧的命名空间版本覆盖,我再次收到错误.

Now when I run the project, I get errors in the ".g.cs" files which say that the namespace is incorrect. I change this namespace but when I compile and run again, the ".g.cs" files get regenerated and overwritten with the old namespace version and I get the error again.

这些文件是什么,我怎样才能让它们从当前文件重新生成,而不是一些明显保存在某处的缓存?

What are the files and how can I make them be regenerate from the current files instead of some cache that its obviously holding somewhere?

我删除了 \bin 和 \obj 目录并重新构建,但仍然出现错误.

I deleted the \bin and \obj directories and rebuilt but still get the errors.

推荐答案

.g.cs 文件是从 .xaml 文件生成的.您需要更改 .xaml 文件中的 x:class= 属性以匹配新的命名空间限定的类名;那么 .g.cs 将在下次编译时正确生成.(不要手动更改 .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.)

例如,如果您之前在 .cs 中有此内容:

For example, if you previously had this in your .cs:

namespace Foo {
    class Bar { ...

这在您的 .xaml 中:

and this in your .xaml:

<UserControl x:Class="Foo.Bar" ...

然后你改变了你的命名空间:

And then you changed your namespace:

namespace Baz {
    class Bar { ...

然后您需要将 .xaml 文件更改为:

Then you would need to change your .xaml file to:

<UserControl x:Class="Baz.Bar" ...

这篇关于更改 UserControl 文件中的命名空间后,...g.cs 文件出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆