首页 > 解决方案 > 引用的外部命名空间与本地命名空间崩溃

问题描述

我有一个名为 的项目This.Is.My.Project,其中包含一个名称空间This.Is.My.Project.Namespace。此命名空间包含一个 UserControl 类,该类派生自名为This.Is.Another.Project.Namespace.

namespace This.Is.My.Project.Namespace {
   public partial class MyClass : This.Is.Another.Project.Namespace.ExternalClass {
     ...
   }
}

现在编译器在这个词下划线Another并抱怨以下消息:

命名空间“This.Is.My.Project.This.Is”中不存在类型或命名空间名称“Another”(您是否缺少程序集引用?)

显然,编译器正在我的本地命名空间中寻找外部类,因为第一个“子”命名空间,即 ' This.Is' 是相等的。虽然实际上它应该更像是项目名称是一个单一的命名空间而不是四个嵌套的命名空间,就像这样:(I.Am.Not.Nested).ActualNamespace.MyClassso in my case (This.Is.My.Project).Namespace.MyClass

我可以通过包含来摆脱错误背后的代码,using This.Is.Another.Project.Namespace但我在 Xaml 方面遇到了同样的问题,我无法使用任何包含。

<ext:ExternalClass x:Class="This.Is.My.Project.Namespace.MyClass"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                   xmlns:ext="http://schemas.external.com/winfx/2008/xaml/external">
</dxe:ComboBoxEdit>

我可以手动将命名空间从This.Is.My.Project.Namespaceto重命名That.Is.My.Project.Namespace并消除错误,但我希望命名空间听起来尽可能接近,因为用户控件的行为非常接近。如果可能的话,我什至会将该类添加到外部命名空间。有没有办法可以保留我的命名空间名称并摆脱这个错误,或者我必须重命名我的命名空间?

标签: wpfnamespacesuser-controls.net-assembly

解决方案


如果您在该视图后面的代码中添加 using 怎么办,那么它是否接受 xaml 中的命名空间?

顺便说一句,使用最能代表其含义的不同名称空间命名不同的程序集是一种很好的做法。


推荐阅读