WPF:XAML自定义命名空间

时间:2009-03-21 16:33:03

标签: wpf xaml namespaces

好的,我在WPF中有一个Window。我在其中添加以下行:

xmlns:controls="clr-namespace:mCubed.Controls"

这编译并运行得很好,但Visual Studio设计器给了我这个错误:

  

无法加载文件或程序集'mCubed,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一。系统找不到指定的文件。

当我从Window中删除这一行时,它编译并运行得很好,而Visual Studio设计师就像一个魅力!

我很困惑为什么那一行打破了设计师?如果我将以下行放在XAML文档的正文中,则会发生 REGARDLESS

<controls:MyControl/>

我所有的.cs文件都在同一个VS项目中。我有一个mCubed命名空间,其中包含我巧妙命名的mCubedWindow类。我在mCubed.Controls命名空间中定义了所有控件类。不要告诉我这是一个装配问题,所有我的文件都在同样的项目中!

4 个答案:

答案 0 :(得分:5)

不是装配问题,只是设计师问题。 2008年的VS WPF设计师充其量是原始的 - 完全无用的恕我直言。我完全关闭它并使用XML编辑器。希望2010年情况会有很大改善。

答案 1 :(得分:2)

MyControl与窗口在同一个程序集中吗?如果不是,则需要在声明中包含程序集名称:

xmlns:controls="clr-namespace:mCubed.Controls;assembly=mCubed"

答案 2 :(得分:1)

这有点奇怪。我已经开发了几个完全正确的项目。这是一个快速的虚拟项目,一个一个.exe:

首先,UserControl带有几个按钮:

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid Width="30">
        <Button HorizontalAlignment="Left">A</Button>
        <Button HorizontalAlignment="Right">B</Button>
    </Grid>
</UserControl>

现在是主窗口,添加了我的控件:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:p="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <p:UserControl1/>
    </Grid>
</Window>

任何地方都没有错误消息。

答案 3 :(得分:0)

XAML是否松散(构建操作:无,无代码)或已编译(构建操作:页面,后面可能有代码)?

如果XAML松动或MyControl在不同的程序集中,您必须指定MyControl所在的程序集,如Daniel Pratt所说:

xmlns:controls="clr-namespace:mCubed.Controls;assembly=mCubed"

确保将程序集mCubed及其依赖项(引用)复制到输出目录中。如果不是,则添加mCubed作为启动项目的参考。