名称在命名空间中不存在。

7
我正在使用数据模板绑定在VS 2015上开发一个简单的UWP项目。每当我尝试为DataTemplate指定类型时,就会出现错误。
XAML:
<Page x:Name="RootPage"
x:Class="Adaptive_News_Layout.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:Adaptive_News_Layout"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" FontSize="22" >

 <SplitView x:Name="MySplitView" Grid.Row="1" DisplayMode="CompactOverlay" Background="LightGray" OpenPaneLength="200"  >
            <SplitView.Pane>
                <ListView x:Name="MyListview" ItemsSource="{x:Bind NavigationItems}"  >
                    <ListView.ItemTemplate>
                        <DataTemplate x:DataType="local:NavItem" >
                            <StackPanel Orientation="Horizontal">
                                <RelativePanel>
                                    <Button x:Name="Icon"  FontFamily="Segoe MDL2 Assets" Content="{x:Bind ButtonIcon}" Width="50" Height="50"/>
                                    <TextBlock x:Name="Section" Text="{x:Bind SectionTitle}" RelativePanel.RightOf="Icon" />
                                </RelativePanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>

这是课程:
namespace Adaptive_News_Layout
{
    public class NavItem
    {
        public string ButtonIcon { get; set; }
        public string SectionTitle { get; set; }
    }
}

错误信息为:在命名空间 "using:Adaptive_News_Layout" 中不存在名称为 "NavItem" 的元素。

“local” 如何声明? - AlexDrenea
尝试构建解决方案,查看是否存在任何构建错误。然后重新启动VS,检查错误是否仍然存在。 - Elvis Xia - MSFT
这里情况也一样,你解决了吗? - Matthew Optional Meehan
xmlns:local="using:Adaptive_News_Layout" - Uncle Ben
这些错误真的很烦人,浪费了很多时间>< - visc
4个回答

8
我找到了问题所在。这是Visual Studio 2015中的一个故障。在XAML中添加命名空间后,最好编译/测试运行程序,否则会出现此问题。要修复它,只需按照以下步骤进行操作:
  1. 删除相关的命名空间引用及其所有用法。
  2. 进行测试运行/编译程序。
  3. 将命名空间引用添加回开放页面标记中。
  4. 再次进行测试运行/编译程序。
现在,当您使用新的命名空间引用时,编译器不会出现故障。

2
仍然发生在VS2019中。在我的情况下,命名空间存在,但我自从添加了新类以来就没有编译过。删除有问题的XAML,编译,重新添加它,错误消失了。 - Geordie

1
我所做的是在命名空间引用中指定程序集,即: xmlns:the_namespace="clr-namespace:the_namespace" - 会产生上述错误。 xmlns:the_namespace="clr-namespace:the_namespace;assembly=the_assembly" - 可以正常工作。

0

我发现我无意中在一个子命名空间中定义了相同的对象。一旦我摆脱了第二个定义,这个问题就消失了。


-1
你需要在 Xaml 文件的顶部的 <Window> 标签中声明 'local' 命名空间。 你会看到很多格式为 xmlns:Name="value" 的命名空间。 添加你的命名空间,Name 为 local,Value 为你的命名空间。

这是我程序的开头标签:其中声明了“local”命名空间'<Page x:Name="RootPage" x:Class="Adaptive_News_Layout.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Adaptive_News_Layout" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" FontSize="22" >' - Uncle Ben
第五行读作 **xmlns:local="using:Adaptive_News_Layout"**。 - Uncle Ben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接