2013-01-01 45 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