简体   繁体   English

摆脱Visual Studio命名空间错误消息

[英]Get rid of Visual Studio Namespace error message

this is unfortunately a very beginner question: 不幸的是,这是一个非常初学者的问题:

I am doing the very simple WPF tutorials and I am stuck on a namespace problem. 我正在做非常简单的WPF教程,并且遇到了名称空间问题。 I want to do a simple hierarchical treeview binding on a custom object according to the tutorial. 我想根据教程对自定义对象进行简单的分层树视图绑定。 I put the object into a custom namespace "MyNameSpace" and declared this in XAML ( xmlns:MyTree="clr-namespace:MyNameSpace"). 我将对象放入自定义名称空间“ MyNameSpace”,并在XAML中声明了该名称(xmlns:MyTree =“ clr-namespace:MyNameSpace”)。 I believe I don't need to specify the assembly as I am just in my project without any further reference (new and clean project). 我相信我不需要指定程序集,因为我只是在我的项目中而无需任何进一步的参考(新的和干净的项目)。

The problem I have now, is that the compiler gives me an error at 我现在遇到的问题是,编译器在以下位置给我一个错误

 <HierarchicalDataTemplate  DataType="{x:Type MyTree:MenuItemNew}"

with the message 与消息

The name "MenuItemNew" does not exist in the namespace "clr-namespace:MyNameSpace" 名称“ MenuItemNew”在名称空间“ clr-namespace:MyNameSpace”中不存在

But it does exist! 但是它确实存在! AND it even compiles and starts the program correctly. 并且它甚至可以编译并正确启动程序。 However, I cannot see the layout anymore because of "Invalid Markup". 但是,由于“无效标记”,我再也看不到布局。

So how can I tell XAML to accept my namespace? 那么如何告诉XAML接受我的名称空间? Or what would be a best way to solve this? 还是解决此问题的最佳方法是什么?

Here is the 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>

and here is my MainWindow Code: 这是我的MainWindow代码:

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; }
    }
}

It seems to me that your main application namespace is TreeViewTestC and not MyNameSpace . 在我看来,您的主要应用程序名称空间是TreeViewTestC不是 MyNameSpace Therefore, you may well need to tell the compiler that your MyNameSpace is actually in the TreeViewTestC assembly, despite you only having a single project. 因此,即使您只有一个项目,也可能需要告诉编译器您的MyNameSpace实际上在TreeViewTestC程序MyNameSpace Try using this instead: 尝试使用此代替:

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

Ok, I found the solution. 好的,我找到了解决方案。 The project was on a network drive. 该项目在网络驱动器上。 Moving it to a local folder solves the issues... Man, what a mess. 将其移动到本地文件夹可以解决问题……伙计,真是一团糟。 This has cost me so much time and I need the network drive.. 这花了我很多时间,我需要网络驱动器。

More info: The name "XYZ" does not exist in the namespace "clr-namespace:ABC" 更多信息: 名称空间“ clr-namespace:ABC”中不存在名称“ XYZ”

Thanks a lot for your help 非常感谢你的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM