【问题标题】:Access namespace not defined exactly in XAML访问命名空间未在 XAML 中准确定义
【发布时间】:2016-11-22 05:57:41
【问题描述】:

假设我的 XAML 在 mycontrols 中使用导入的命名空间定义如下。

<Grid x:Class="LayoutGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"         
    xmlns:mycontrols="clr-namespace:Predefined.Controls;assembly=UIControls"  
    >

如果我要使用命名空间 Predefined.Controls 中定义的东西,那么我会简单地用它的别名 mycontrols 来引用它

例子:

<mycontrols:MyCustomButton Name="SubmitButton" />

现在如果命名空间Predefined.Controls.CustomTextBoxes 也存在,有没有办法在这个命名空间内使用控件而不必将它添加到顶部的 XAML 定义中?

这样的??

<mycontrols.CustomTextBoxes:MyCustomTextBox Name="TextBox1" />

【问题讨论】:

    标签: c# .net wpf xaml namespaces


    【解决方案1】:

    没有。在 XML 中,名称空间前缀定义了名称空间,您不能只是在其上添加东西。您需要将完整的 CLR 命名空间作为 XML 命名空间声明添加到父元素中:

    xmlns:ctb="clr-namespace:Predefined.Controls.CustomTextBoxes;assembly=UIControls"
    

    并在您在 XAML 中实例化该元素/控件时使用该前缀:

    <ctb:MyCustomTextBox />
    

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 2012-03-26
      • 2019-10-21
      相关资源
      最近更新 更多