为什么向用户控件添加 x:Name 属性会导致编译错误?

发布于 2024-09-11 22:49:49 字数 932 浏览 4 评论 0原文

我真的需要更多关于造成这种情况的想法,目前它让我陷入困境。

我有一个 Xaml 用户控件,其中包含另一个用户控件,如下所示:


<UserControl x:Class="MyModule.View.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:igDock="http://infragistics.com/DockManager"
    xmlns:views="clr-namespace:MyModule.View"
    >
  <StackPanel x:Name="panel">
    <views:MyHeaderView/>
    <igDock:XamDockManager x:Name="dockingManager"/>
  </StackPanel>
</UserControl>

这是在 Prism 模块内(因此是 MyModule 名称),但除此之外,我不认为这有什么特别不寻常的地方。

现在,一旦我向标头视图添加名称,代码就无法编译,例如

<views:MyHeaderView x:Name="header"/>

我得到的错误很简单:

"类型名称 'View' 在类型 MyModule.MyModule' 中不存在"< /code>

我尝试将包含的用户控件移动到另一个命名空间中,创建一个空白用户控件并尝试使用它(相同的结果)并删除 XamDockManager,但似乎没有任何区别。

任何人都可以阐明这一点吗?

I really need some more ideas as to what's causing this, currently it's driving me up the wall.

I have a Xaml user control which contains another user control like this :


<UserControl x:Class="MyModule.View.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:igDock="http://infragistics.com/DockManager"
    xmlns:views="clr-namespace:MyModule.View"
    >
  <StackPanel x:Name="panel">
    <views:MyHeaderView/>
    <igDock:XamDockManager x:Name="dockingManager"/>
  </StackPanel>
</UserControl>

This is within a Prism module (hence the MyModule name), but otherwise I don't think this is anything particularly unusual.

Now, as soon as I add a name to the header view the code fails to compile, e.g.

<views:MyHeaderView x:Name="header"/>

The error I get back is simply :

"The type name 'View' does not exist in the type MyModule.MyModule'"

I've tried moving the contained user control into another namespace, creating a blank user control and trying with that (same result) and removing the XamDockManager, but nothing seems to make any difference.

Can anyone shed any light on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

○愚か者の日 2024-09-18 22:49:49

我想我找到了答案。

如果我将模块类重命名为 MyModuleThingy,则它可以编译。似乎出现问题是因为模块类与包含它的命名空间具有相同的名称(MyModule.MyModule)。

如果有人能让我知道为什么这可能是一个问题,我很有兴趣知道。特别是因为我继承了很多代码,这似乎是 Prism 特定实现中的常见模式。

I think I found the answer.

If I rename the module class to MyModuleThingy, then it compiles. It seems the problem arises because the module class has the same name as the namespace which contains it (MyModule.MyModule).

If anyone can let me know why that might be a problem, I'd be very interested to know. Particularly because I've inherited a lot of code where that seems to be the common pattern within this particular implementation of Prism.

问题是您有一个同名的类和命名空间。如果你有类似的内容:

namespace MyModule {
    public class MyModule {
    }
    public class MyView {
    }
    public class MyControl {
        public void Foo() {
            MyModule.MyView v = ..;
        }
    }
}

v 之前的类型会认为 MyView 作为嵌套类型在 MyModule 类型中。它不会将 MyModule 解析为命名空间,因为该类型根据类型 解析获胜C# 的规则

后面生成的代码只是采用您在 xmlns 中定义的命名空间,并在创建支持字段时将其添加到前面(由于 x:Name 属性)。因此,您最终会得到 MyModule.View.MyHeaderView,其中 MyModule 被解释为类型,而不是命名空间。

The issue is you have a class and namespace with the same name. If you have something like:

namespace MyModule {
    public class MyModule {
    }
    public class MyView {
    }
    public class MyControl {
        public void Foo() {
            MyModule.MyView v = ..;
        }
    }
}

The type before v will think MyView is in the MyModule type as a nested type. It does not resolve MyModule as a namespace, since the type wins based on the type resolution rules of C#.

The generated code behind is simply taking the namespaces you define in the xmlns and prepending it when creating the backing field (due to the x:Name attribute). So you end up with MyModule.View.MyHeaderView, where MyModule is interpreted as the type, not the namespace.

一袭水袖舞倾城 2024-09-18 22:49:49

我很不幸地把我的项目称为 TextEditor,而且显然这个名称与现有的 MS 类冲突。我尝试将 x.Name 或 Name 属性添加到对用户控件的引用时出现编译错误,抱怨编译器找不到我的用户控件。
显然它正在研究 MS 类,而不是本地项目。
我将项目重命名为 TextEditorProject,必须使用 Grep 来查找所有出现的情况,但最终,我让它工作了。

I was unlucky enogh to call my project TextEditor, and apparently the name conflicted with an exiting MS class. My attempts to add a x.Name or Name attribute to a reference to my user control gave me compilation error complaining that compiler can not find my user control.
Apparently it was looking into MS class, and not into the local project.
I renamed the project into TextEditorProject, had to use Grep to find all the occurances, but eventually, I got it working.

递刀给你 2024-09-18 22:49:49

难道你有一个名为“View”的类,与命名空间相同?

Could it be that you have class that is named "View", same as namespace?

雾里花 2024-09-18 22:49:49

WPF 不喜欢您使用 x:Name 作为程序集中定义的控件,假设 xmlns:views="clr-namespace:MyModule.View" 与您的程序集相同

用名字代替,你会没事的

WPF doesn't like you using x:Name for a control defined in your assembly, assuming xmlns:views="clr-namespace:MyModule.View" is the same assembly as your

Use name instead, and you will be fine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文