【问题标题】:The name does not exist in the namespace despite being there该名称在命名空间中不存在,尽管存在
【发布时间】:2020-06-02 22:49:35
【问题描述】:

我正在尝试使用数据绑定来创建列表视图,但出现此错误。为什么即使受影响的名称实际上在受影响的类中,也会出现以下错误?

命名空间“using:My_App.Models”中不存在名称“ListItem”。

MainPage.xaml

<Page
    x:Class="My_App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:My_App.Models"
    mc:Ignorable="d">

        <ListView 
            ItemsSource="{x:Bind listItems}" 
            ItemClick="ListView_ItemClick"
            SelectionChanged="ListView_SelectionChanged">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="data:ListItem">
                    <StackPanel>
                        <TextBlock Text="{Binding BookTitle}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
</Page>

MainPage.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using My_App.Models;
using Windows.ApplicationModel.Email;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace My_App
{
    public sealed partial class MainPage : Page
    {
        private List<ListItem> listItems;

        public MainPage()
        {
            this.InitializeComponent();

            listItems = ItemManager.GetListItems();
        }
    }
}

ListItem 类(在“模型”文件夹内)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace My_App.Models
{

    public class ListItem
    {
        public string BookTitle { get; set; }
    }

    public class ItemManager
    {
        public static List<ListItem> GetListItems()
        {
            var items = new List<ListItem>
            {
                new ListItem { BookTitle = "The Wizard of Oz" }
            };

            return items;
        }
    }
}

【问题讨论】:

  • 为什么在xmlns 中需要using
  • 请检查xmlns:data="using:My_App.Models"是否是正确的参考。我用上面做了一个代码示例,你的代码看起来是正确的。

标签: c# xaml uwp uwp-xaml


【解决方案1】:

该名称在命名空间中不存在,尽管存在

为了检查您的代码,我发现您错过了 MainPage 类中的 My_App.Models 命名空间。我做了一个代码sample here,效果很好,请检查一下。

如果问题仍然存在,请尝试删除项目的 bin 和 obj 文件夹并在启动应用前构建解决方案。

【讨论】:

    【解决方案2】:

    这很奇怪,但是将 Binding 更改为 x:Bind 有助于编译项目。 此外,private List&lt;ListItem&gt; listItems; 还有第二个错误。它必须是公开的。所以:

    <StackPanel>
          <TextBlock Text="{x:Bind BookTitle}" />
    </StackPanel>
    

    public List<ListItem> listItems;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 2013-01-17
      • 1970-01-01
      相关资源
      最近更新 更多