简体   繁体   English

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

[英]VS2012 Xaml C# The name does not exist in the namespace

I'm trying to do this simple tutorial and I'm upto the second part: http://msdn.microsoft.com/en-us/library/cc265158(v=vs.95).aspx 我正在尝试做这个简单的教程,第二部分: http : //msdn.microsoft.com/zh-cn/library/cc265158( v=vs.95) .aspx

(In my code I've just replaced Customer with Game (在我的代码中,我刚刚将Customer替换为Game

but I keep getting the errors: The name "Games" does not exist in the namespace 但我不断收到错误消息:名称空间中不存在“游戏”名称

"clr-namespace:GameLauncher". “ clr-namespace:GameLauncher”。 Unknown type 'Games' in XML namespace 'clr-namespace:GameLauncher;assembly=GameLauncher, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' XML名称空间'clr-namespace:GameLauncher; assembly = GameLauncher,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'中未知类型'Games'

My code for the XAML is: 我的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>

and my C# code is: 而我的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)
    {
    }
}
}

I'm most likely doing something stupidly wrong, I haven't coded in quite a while. 我很可能在做一些愚蠢的错误,我已经有一段时间没有编码了。

I had it worked for one second, then I when I was changing it from Customers to Games, it all stopped working and I couldn't get it working again, even if I changed it back to customers, even if I started the project from scratch again. 我让它工作了一秒钟,然后当我将它从“客户”更改为“游戏”时,一切都停止了工作,即使我将它改回了客户,即使我从再次刮擦。

I don't see where you are creating the Games object within your code. 我看不到代码中在哪里创建Games对象。 You will need a Games property with a getter/setter in order to use it in the XAML as you are. 您将需要一个带有getter / setter的Games属性,以便按原样在XAML中使用它。

Within MainPage() you need to do something like this: 在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)
    {
    }
}

As a point of clarity I would use something like 'AllGames', 'CurrentGame', etc. instead of just Games. 为了清楚起见,我将使用诸如“ AllGames”,“ CurrentGame”等之类的东西,而不仅仅是游戏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LayoutAwarePage在命名空间VS2012中不存在bug? - LayoutAwarePage does not exist in namespace VS2012 bug? C#WPF-名称“…”在命名空间“ clr-namespace:…”中不存在 - C# WPF - The name “…” does not exist in the namespace “clr-namespace:…” c#错误名称空间中不存在类型或名称空间名称&#39;SampleMain&#39; - c# error The type or namespace name 'SampleMain' does not exist in the namespace WPF:名称XYZ在XAML命名空间中不存在 - WPF: The name XYZ does not exist in XAML namespace 名称空间WPF XAML文件中不存在名称“” - The name “ ” does not exist in the namespace WPF XAML file XAML 中的“名称空间错误中不存在名称” - "The name does not exist in the namespace error" in XAML VS2012中的DLL加载(C#/ IronPython / C#) - DLL loading (C#/IronPython/C#) in VS2012 VS2012项目Xaml报告找不到命名空间错误,但项目编译正常 - VS2012 Project Xaml reports cannot find namespace error, but project compiles fine VS 2017 c#错误CS0234命名空间中不存在类型或命名空间名称(您是否缺少程序集引用?) - VS 2017 c# Error CS0234 The type or namespace name does not exist in the namespace (are you missing an assembly reference?) AutoComplete comboBox不建议何时将文本复制并粘贴到C#Winform VS2012中的comboBox中 - AutoComplete comboBox does not suggest when the text copied and pasted into comboBox in c# winform VS2012
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM