c# - 命名空间中不存在名称

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

我正在使用 VS 2015 中的数据模板绑定(bind)开发一个简单的 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”中不存在

最佳答案

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

<醇>
  • 删除有问题的 namespace 引用和该引用的所有用法。
  • 执行测试运行/编译您的程序。
  • 将命名空间引用添加回起始页面标记
  • 执行另一次测试运行/编译您的程序。
  • 现在,当您使用新的 namespace 引用时,编译器不会出现故障。

    关于c# - 命名空间中不存在名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38194074/

    相关文章:

    visual-studio - 如何在没有 Visual Studio 设计器的情况下查看 XAML 控件的默认模板和样式?

    c# - 在 C# UWP Windows 10 应用程序中读取和写入 Excel 工作表

    wpf - 具有默认模板和 itemTemplate 的递归菜单项

    c# - 使用 C# 将 HTML 控件(Div 或表)转换为图像

    c# - 如何在Windows Phone 10中检测电话挂断

    c# - 如何获得最佳数字格式字符串?

    c# - 在 DataTemplate 中排列 ItemsControl 项

    WPF - DataTemplates 的参数?

    c# - 修复了 gridview 中的标题列宽度?

    c# - 如何手动验证 JWT Asp.Net Core?