【问题标题】:Get rid of Visual Studio Namespace error message摆脱 Visual Studio 命名空间错误消息
【发布时间】:2015-09-03 22:18:08
【问题描述】:

不幸的是,这是一个非常初级的问题:

我正在做非常简单的 WPF 教程,但遇到了命名空间问题。 我想根据教程对自定义对象进行简单的分层树视图绑定。我将对象放入自定义命名空间“MyNameSpace”并在 XAML 中声明(xmlns:MyTree="clr-namespace:MyNameSpace")。我相信我不需要指定程序集,因为我只是在我的项目中,没有任何进一步的参考(新的和干净的项目)。

我现在遇到的问题是编译器在

处给了我一个错误
 <HierarchicalDataTemplate  DataType="{x:Type MyTree:MenuItemNew}"

与消息

命名空间“clr-namespace:MyNameSpace”中不存在名称“MenuItemNew”

但它确实存在!它甚至可以正确编译和启动程序。但是,由于“无效标记”,我再也看不到布局了。

那么我怎样才能告诉 XAML 接受我的命名空间呢?或者什么是解决这个问题的最佳方法?

这是 XAML:

<Window x:Class="TreeViewTestC.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:MyTree="clr-namespace:MyNameSpace"
        Title="MainWindow" Height="350" Width="525">
    <Grid Margin="10">
        <TreeView Name="trvMenu">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate  DataType="{x:Type MyTree:MenuItemNew}" ItemsSource="{Binding Items}">
                    <TextBlock Text="{Binding Title}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

这是我的主窗口代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Collections.ObjectModel;
using MyNameSpace;

namespace TreeViewTestC
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MenuItemNew root = new MenuItemNew() { Title = "Menu1" };
            MenuItemNew childItem1 = new MenuItemNew() { Title = "Child item #1" };
            childItem1.Items.Add(new MenuItemNew() { Title = "Child item #1.1" });
            childItem1.Items.Add(new MenuItemNew() { Title = "Child item #1.2" });
            root.Items.Add(childItem1);
            root.Items.Add(new MenuItemNew() { Title = "Child item #2" });
            trvMenu.Items.Add(root);
        }
    }
}

namespace MyNameSpace
{
    public class MenuItemNew
    {
        public MenuItemNew()
        {
            this.Items = new ObservableCollection<MenuItemNew>();
        }

        public string Title { get; set; }

        public ObservableCollection<MenuItemNew> Items { get; set; }
    }
}

【问题讨论】:

  • 你得到什么错误?
  • 看起来很傻,但是您是否尝试过清理并重建?我完全复制了你的代码,做了一个构建,错误就消失了。
  • 我清理并重建了它,它仍然存在。我使用 Visual Studio 2012,但我认为没有服务包。我无法在此处轻松访问更新。所以 XAML 基本上是正确的,对吧?
  • 有时我会遇到这样的问题并通过运行“清洁解决方案”来解决它们,然后重新启动 Visual Studio,然后再次构建。您也可能在 Visual Studio 中遇到了可能会在更新中修复的错误。

标签: c# .net wpf xaml namespaces


【解决方案1】:

在我看来,您的主要应用程序命名空间是TreeViewTestC不是 MyNameSpace。因此,您可能需要告诉编译器您的MyNameSpace 实际上在TreeViewTestC 程序集中,尽管您只有一个项目。尝试改用这个:

xmlns:MyTree="clr-namespace:MyNameSpace;assembly=TreeViewTestC"

【讨论】:

  • 在我清理和重建之前似乎工作正常。然后这再次给出错误“类型引用找不到名为'MenuItemNew'的公共类型”
  • 我升级到 Visual Studio 2013 Update 4。仍然是同样的错误。 (不管我有没有组装)
【解决方案2】:

好的,我找到了解决方案。该项目位于网络驱动器上。将其移动到本地文件夹可以解决问题... 伙计,真是一团糟。这花费了我很多时间,我需要网络驱动器..

更多信息: The name "XYZ" does not exist in the namespace "clr-namespace:ABC"

非常感谢您的帮助

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    相关资源
    最近更新 更多