我正在尝试将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">
答案 0 :(得分:3)
这会为你做的 -
<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">
对于您在上面的帖子中报告的错误,只需从命名空间声明中删除程序集属性。
答案 1 :(得分: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">