【问题标题】:Name does not exist in the namespace名称空间中不存在名称
【发布时间】:2016-11-06 17:43:05
【问题描述】:

我正在使用 VS 2015 中的数据模板绑定开发一个简单的 UWP 项目。当我尝试指定数据模板的类型时,我会收到错误。

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

错误内容为:名称“NavItem”在命名空间“using:Adaptive_News_Layout”中不存在

【问题讨论】:

  • “本地”是如何声明的?
  • 尝试构建解决方案,看看是否有任何构建错误。并重新启动 VS 检查错误是否仍然存在。
  • 同样的情况,你搞定了吗?
  • xmlns:local="using:Adaptive_News_Layout"
  • 这类错误好烦,浪费时间>

标签: c# uwp datatemplate visual-studio-debugging uwp-xaml


【解决方案1】:

我发现了问题所在。这是 Visual Studio 2015 中的一个小故障。在 XAML 中添加命名空间后,最好编译/测试运行您的程序,否则您将遇到此问题。修复它:

  1. 删除有问题的命名空间引用以及该引用的所有用法。
  2. 执行测试运行/编译您的程序。
  3. 将命名空间引用重新添加到起始页标记中
  4. 执行另一个测试运行/编译您的程序。
现在,当您使用新的命名空间引用时,编译器不会出现故障。

【讨论】:

  • 在 VS2019 中仍然存在。在我的例子中,命名空间存在,但我没有编译,因为我添加了新的类。删除了有问题的 XAML,编译,重新添加它并且错误消失了。
【解决方案2】:

对我有用的是在命名空间引用中指定程序集,即: xmlns:the_namespace="clr-namespace:the_namespace" - 产生上述错误。 xmlns:the_namespace="clr-namespace:the_namespace;assembly=the_assembly" - 效果很好。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      您必须在 ypur Xaml 文件顶部的部分中声明“本地”命名空间。 您将看到许多格式为 xmlns:Name ="value" 的命名空间 使用 Name =local 和 Value 添加您的命名空间作为您的命名空间

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2012-12-06
      • 2016-11-29
      • 1970-01-01
      相关资源
      最近更新 更多