2013-01-01 46 views
0

我试图做这个简单的教程存在,我是高达第二部分: http://msdn.microsoft.com/en-us/library/cc265158(v=vs.95).aspxVS2012的XAML C#的名字没有出现在命名空间

(在我的代码,我刚刚更换客户与游戏

,但我不断收到错误: 命名为“游戏”没有命名空间中存在

“CLR的命名空间:GameLauncher” 未知类型“游戏”中的XML命名空间“CLR的命名空间。 :GameLauncher;装配= GameLaun雪儿,版本= 1.0.0.0,文化=中立,公钥=空”

我的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) 
    { 
    } 
} 
} 

我最有可能做一些愚蠢的错误,我还没有编码很长一段时间。

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

+0

您确定Games类在GameLauncher命名空间内吗? –

+0

我已经完成了所有我已经完成的代码,它看起来在我看来。 – user1940572

+0

在教程中他们使用UserControl而不是Page,你有没有试过?如果您不打算使用导航,只需使用Window或UserControl(在窗口中显示)。 – Hannish

回答

0

我看不到您在代码中创建游戏对象的位置。您将需要一个带有getter/setter的游戏属性,以便按原样在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”等,而不仅仅是游戏。

+0

感谢这是迄今为止信息量最大的答案,但我仍然无法正常工作。我所有的代码和我上一篇文章完全一样,但是我包含了你的新增内容。 – user1940572

+0

我能够以不同的方式使用它,我在我的ListBox中添加了x:Name =“Main”到并添加了ItemsSource =“{Binding ElementName = Main,Path = AllGames}”。 这是检索列表的好方法吗?它正在工作。 – user1940572

+0

可能是您的xaml上的DataContext没有设置为您的MainWindow的DataContext,在这种情况下,它赢得了 – BrianKE