【问题标题】:Enum being from imported namespace bound to ListBox cannot be found无法找到来自绑定到 ListBox 的导入命名空间的枚举
【发布时间】:2011-06-23 14:28:31
【问题描述】:

我花了很多时间研究将枚举绑定到列表框的代码,并且在以下情况下我很好:

  1. 与 XAML 位于同一命名空间中的枚举
  2. 枚举位于以 Windows.etc. 开头的命名空间中。 但是,我有一个包含在 Microsoft.Research.Kinect.Nui 命名空间命名空间中的枚举:

    <Window.Resources>
        <ObjectDataProvider MethodName="GetValues"
                        ObjectType="{x:Type sys:Enum}"
                        x:Key="Joints">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="JointID" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    

    在我设置方法参数类型的那一行,我得到一个错误

找不到类型“JointID”

我知道这与设置 clr-namespace 路径有关:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:mic="clr-namespace:Microsoft;assembly=Microsoft.Research.Kinect"

(管理 API 的程序集是 Microsoft.Research.Kinect.dll) 但是当我这样做时,我得到一个错误:

未定义的 CLR 命名空间。 “clr-namespace” URI 指的是程序集中未包含的命名空间“Microsoft”。

怎么办?

【问题讨论】:

    标签: c# .net wpf visual-studio xaml


    【解决方案1】:
    <Window 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:mic="clr-namespace:Microsoft;assembly=Microsoft.Research.Kinect"
            xmlns:local="clr-namespace:YOUR NAMESPACE" >
       <Window.Resources>
          <ObjectDataProvider MethodName="GetValues"
                              ObjectType="{x:Type sys:Enum}"
                              x:Key="Joints">
             <ObjectDataProvider.MethodParameters>
                <x:Type Type="{x:Type local:JointID" />
             </ObjectDataProvider.MethodParameters>
          </ObjectDataProvider>
       </Window.Resources>
    </Window>
    

    尝试绑定类型。您必须在顶部添加命名空间,并且枚举必须是公开的。那我觉得你应该可以参考一下。

    【讨论】:

      【解决方案2】:

      The documentation 说明您需要使用前缀来指定正确的 xml/xaml 命名空间。您还需要定义新的命名空间并确保指定正确的程序集和 .net 命名空间。

      xmlns:kin="clr-namespace:Microsoft.Research.Kinect.Nui;assembly=Microsoft.Research.Kinect"
      
      <Window.Resources>
          <ObjectDataProvider MethodName="GetValues"
                          ObjectType="{x:Type sys:Enum}"
                          x:Key="Joints">
              <ObjectDataProvider.MethodParameters>
                  <x:Type TypeName="kin:JointID" />
              </ObjectDataProvider.MethodParameters>
          </ObjectDataProvider>
      </Window.Resources>
      

      PS:另见wpf binding combobox to enum in different namespace

      【讨论】:

        猜你喜欢
        • 2020-01-04
        • 2019-07-22
        • 1970-01-01
        • 2011-09-17
        • 2011-10-28
        • 2022-01-22
        • 2010-09-09
        • 2012-09-23
        • 1970-01-01
        相关资源
        最近更新 更多