VS2012 Xaml C#名称空间中不存在该名称

时间:2013-01-01 05:58:22

标签: c# xaml

我正在尝试做这个简单的教程,而我是第二部分: http://msdn.microsoft.com/en-us/library/cc265158(v=vs.95).aspx

(在我的代码中,我刚用游戏取代了客户

但我不断收到错误: 名称空间“游戏”在名称空间

中不存在
  

“CLR-名称空间:GameLauncher”。   XML命名空间中的未知类型“游戏”'clr-namespace:GameLauncher; assembly = GameLauncher,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'

我的XAML代码是:

<Page
x:Class="GameLauncher.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GameLauncher"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:src="clr-namespace:GameLauncher"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.Resources>
        <src:Games x:Key="games"/>
    </Grid.Resources>
    <ListBox HorizontalAlignment="Left" Height="{Binding ElementName=LayoutRoot, Path=ActualHeight}" Margin="50,50,0,50" VerticalAlignment="Stretch" Width="300"/>
</Grid>

我的C#代码是:

namespace GameLauncher
{

public class Game
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String Address { get; set; }

    public Game(String firstName, String lastName, String address)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Address = address;
    }

}

public class Games : ObservableCollection<Game>
{
    public Games()
    {
        Add(new Game("Michael", "Anderberg",
                "12 North Third Street, Apartment 45"));
        Add(new Game("Chris", "Ashton",
                "34 West Fifth Street, Apartment 67"));
        Add(new Game("Cassie", "Hicks",
                "56 East Seventh Street, Apartment 89"));
        Add(new Game("Guido", "Pica",
                "78 South Ninth Street, Apartment 10"));
    }

}

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }
}
}

我很可能做了一些愚蠢的错误,我很久没有编码了。

我已经工作了一秒钟,然后当我将它从客户更改为游戏时,它全部停止工作,即使我将其更改回客户,我也无法再次工作,即使我开始工作该项目从头开始。

1 个答案:

答案 0 :(得分:0)

我没有看到您在代码中创建Games对象的位置。您将需要一个带有getter / setter的Games属性,以便在XAML中使用它。

在MainPage()中,您需要执行以下操作:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.Games = new Games();    // this will execute the Games constructor 
                                     // and add the games to Games
    }

    // allows you to use 'Games' in your xaml
    public ObservableCollection<Game> Games  
    {
        get;
        set;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }
}

为了清楚起见,我会使用“AllGames”,“CurrentGame”等内容,而不仅仅是游戏。