对于使用c#和xaml的Windows dev新手。学习抢劫里程"蓝皮书"对于win8 dev。
我不断收到此错误消息并且卡住了。有人可以帮帮我或给我一些指示。我在阻止我麻烦的区块添加了评论。 感谢
using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using AddingMachine;
namespace AddingMachine
{
public class Customer
{
public string Name { get; set;}
public string Address{ get; set;}
public int ID { get; set;}
public Customer(string inName, string InAddress, int inID)
{
Name = inName;
Address = InAddress;
ID = inID;
}
}
public class Customers
{
public string Name { get; set;}
public List<Customer> CustomerList;
public Customers(string inName)
{
Name = inName;
CustomerList = new List<Customer>();
}
Customers mailCustomers = new Customers("Mail Order Customers");
private Customers customerList;
public static Customers MakeTestCustomers()
{
string[] firstNames = new string[] { "Rob", "Jim", "Joe", "Nigel", "Sally", "Tim" };
string[] lastsNames = new string[] { "Smith", "Jones", "Bloggs", "Miles", "Wilkinson", "Brown"};
Customers result = new Customers("Test Customers");
int id = 0; foreach (string lastName in lastsNames)
{
foreach (string firstname in firstNames)
{
//Construct a customer name
string name = firstname + " " + lastName;
//Add the new customer to the list
result.CustomerList.Add(new Customer(name, name + "'s House", id));
// Increase the ID for the next customer
id++;
}
}
return result;
// THIS IS WHERE I AM HAVING PROBLEM. "customers" object keep showing error msg that it does not exit in the current context.
customers = Customers.MakeTestCustomers();
foreach (Customer c in customers.CustomerList)
{
TextBlock customerBlock = new TextBlock();
customerBlock.Text = c.Name;
customersStackPanel.Children.Add(customerBlock);
//customers = Customers.MakeTestCustomers(); customerList.ItemsSource = customers.CustomerList;
//customerList = Customers.MakeTestCustomers();
}
}
}
}
XAML
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AddingMachine"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:App1="using:AddingMachine"
xmlns:App11="using:App1"
x:Class="AddingMachine.MainPage"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<!--<StackPanel x:Name="customersStackPanel" HorizontalAlignment="Left" Margin="9,6,0,0" VerticalAlignment="Top" ScrollViewer.BringIntoViewOnFocusChange="True"/>-->
<ListBox Name="customerList" SelectionChanged="customerList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="customersStackPanel" HorizontalAlignment="Left" Margin="9,6,0,0" VerticalAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollMode="Auto">
<TextBlock Text="{Binding Name}"
Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding Address}"
Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Page>
答案 0 :(得分:2)
书籍可能容易出错,直接从书中复制某些内容可能会让您学到很多东西而不是自己尝试。如果书中有错误,那么这本书是错误的,而不是编译器。这本书不是法律,编译器是。
关于您的实际问题,正如 dcastro 指出的那样,您尚未声明名为customers
的变量。也许您打算使用customerList
代替。
当您发现错误时,请花一些时间阅读您的代码,并准确了解正在发生的事情。错误消息通常很清楚。