【问题标题】:The type src:Videogames was not found. Verify that you are not missing an assembly reference找不到类型 src:Videogames。确认您没有丢失程序集引用
【发布时间】:2011-08-16 12:39:33
【问题描述】:

我正在尝试将 Videogame 对象的集合绑定到我的 ListBox,尽管遵循了 MSDN 示例,但我收到了此错误。

<Grid>
    <Grid.Resources>
        //Error is fired here.
        <src:Videogames x:Key="videogames" />

这是我的电子游戏课:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace UpcomingGames
{
    public class Videogame
    {
        public string Name { get; set; }
        public string ReleaseDate { get; set; }
        public string Synopsis { get; set; }
        public string Developer { get; set; }
    }

    public class Videogames : ObservableCollection<Videogame>
    {
        public Videogames()
        {
            Add(new Videogame {                   
                Name = "Fire Emblem", 
                ReleaseDate = "20/4/2011",
                Developer = "Rockstar Games", 
                Synopsis = @"Lorem ipsum dolor...",
            });
            Add(new Videogame { 
                Name = "Fire Emblem", 
                ReleaseDate = "20/4/2011",
                Developer = "Rockstar Games", 
                Synopsis = @"Lorem ipsum dolor...", 
            });
            Add(new Videogame{
                Name = "Fire Emblem",
                ReleaseDate = "20/4/2011",
                Developer = "Rockstar Games",
                Synopsis = @"Lorem ipsum dolor...",
            });
        }
    }
}

我可能做错了什么,我能做些什么来解决这个问题?

我没有手动添加任何命名空间,因为 MSDN 文章没有告诉我。这是我需要做的吗?无论如何,这是 XAML 的当前状态。

<Window x:Class="UpcomingGames.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="286" Width="199">

【问题讨论】:

  • 您可以将您的 xaml 代码粘贴到您添加命名空间 srv 的位置吗?
  • @RV1987:当然,请看我的编辑。
  • @RV1987:我也遇到了这个错误,我怀疑这是导致另一个主要错误的原因。 Error 2 ''src' is an undeclared prefix. Line 7, position 14.' XML is not valid.
  • 正如 Dave 已经提到的,您需要添加引用..

标签: wpf xaml binding listbox datatemplate


【解决方案1】:

这对你有用 -

<Window x:Class="UpcomingGames.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:UpcomingGames"
    Title="MainWindow" Height="286" Width="199">

对于您在上述帖子中报告的错误,只需从您的命名空间声明中删除程序集属性即可。

【讨论】:

  • 好的,这很好用。我只需要删除assembly 位就可以了。有什么原因吗?
  • 我不确定这个问题,但是由于您没有引用外部程序集,因此您是在同一个程序集中编写的。那么,指定程序集名称需要什么。
  • 即使您尝试添加命名空间,只有当您尝试从外部程序集访问命名空间时,xaml 中的智能感知才会添加程序集。
【解决方案2】:

您的 XAML 中缺少这样的引用

xmlns:src="clr-namespace:UpcomingGames;assembly=UpcomingGames"

此引用将位于 XAML 文件的顶部,并将与其他类似:

<Window x:Class="UpcomingGames.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:UpcomingGames;assembly=UpcomingGames"
    Title="MainWindow" Height="286" Width="199">

【讨论】:

  • 感谢您的帮助,现在我收到此错误:The tag 'Videogames' does not exist in XML namespace 'clr-namespace:UpcomingGames;assembly=UpcomingGames'. Line 8 Position 14. 这很令人困惑,因为我的两个类都在同一个 UpcomingGames 命名空间中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 2020-01-09
  • 2011-02-12
相关资源
最近更新 更多