【问题标题】:Compiler error using xmlns:src使用 xmlns:src 的编译器错误
【发布时间】:2013-11-14 19:23:21
【问题描述】:

遇到编译器错误

src:FieldDefUsrFldUsrs

错误 8 XML 命名空间“clr-namespace:Gabe2a”中不存在标记“FieldDefUsrFldUsrs”。第 14 行位置 10。C:\My Dev\CommonSource\Gabriel\Gabe2a\Gabe2Acollection\Gabe2aI55\PageSearchFieldUsrFld.xaml 14 10 Gabe2a

如何解决这个错误?

<Page x:Class="Gabe2a.PageSearchFieldUsrFld"
      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" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      DataContext="{Binding RelativeSource={RelativeSource self}}"
      xmlns:local="clr-namespace:Gabe2a"
      xmlns:src="clr-namespace:Gabe2a"
      ShowsNavigationUI="False"
    Title="PageSearchFieldUsrFld">
    <Page.Resources>
        <src:FieldDefUsrFldUsrs x:Key="MyList"/>
        <HierarchicalDataTemplate DataType = "{x:Type src:League}"
                                ItemsSource = "{Binding Path=Divisions}">
            <TextBlock Text="{Binding Path=Name}"/>
        </HierarchicalDataTemplate>

namespace Gabe2a
{
    public partial class PageSearchFieldUsrFld : Page
    {            
        private List<GabeLib.FieldDefUsrFldUsr> fieldDefUsrFldUsrs = new List<GabeLib.FieldDefUsrFldUsr>();
        public List<GabeLib.FieldDefUsrFldUsr> FieldDefUsrFldUsrs { get { return fieldDefUsrFldUsrs; } }

namespace Gabe2a {
    public class GabeLib : INotifyPropertyChanged
    {
        [Serializable()]
        public class FieldDefUsrFldUsr 
        {

【问题讨论】:

  • 您的 FieldDefUsrFldUsr 似乎不在 Gabe2a 命名空间中。还是嵌套类?
  • 我发布了 FieldDefUsrFldUsr 的定义。 private List 没有抛出错误

标签: .net wpf binding treeview


【解决方案1】:

好吧,您尝试在 XAML 中实例化嵌套类,但在 WPF 中仍然不行。请参阅this answer 了解有关该问题的一些详细信息和 MSDN 链接,该链接说明了要在 XAML 中使用的自定义类的规则:

您的自定义类不能是嵌套类。嵌套类及其常规 CLR 使用语法中的“点”会干扰其他 WPF 和/或 XAML 功能,例如附加属性。

这仍然与框架版本 4.5 相关。

至于如何避免这种情况......你有任何真正的理由使用嵌套类吗?对于项目组织而言,简单地使用点分命名空间通常就足够了:

namespace Gabe2a.GabeLib 
{
    public class GabeLib : INotifyPropertyChanged         
    {}

    [Serializable()]
    public class FieldDefUsrFldUsr 
    {}
}

因此,您似乎无法在 XAML 中实例化嵌套类,但您仍然可以在 DataTemplates 等中使用它,而无需更改当前的类结构:

<DataTemplate DataType="{x:Type local:GabeLib+FieldDefUsrFldUsr}">
    ...
</DataTemplate>

【讨论】:

  • 这对我不起作用。我最终在命名空间级别添加了一个接口。
猜你喜欢
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多