这段代码有什么问题[windows phone 8.1开发]c#和Xaml

本文关键字:phone 开发 Xaml windows 段代码 代码 问题 什么 | 更新日期: 2023-09-27 17:53:04

我是使用c#和xaml的Windows开发新手。学习与rob miles的"蓝皮书"win8开发。

我一直得到这个错误信息,我卡住了。有人能帮我一下或者给我一些建议吗?我在给我带来麻烦的块上添加了一条评论。由于

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>

这段代码有什么问题[windows phone 8.1开发]c#和Xaml

书籍很容易出错,直接从书上抄东西可能不会帮助你学到很多东西,而不是自己尝试。如果书中有错误,那么是书错了,而不是编译器错了。书不是法律,编译器才是。

至于您的实际问题,正如dcastro所指出的,您没有声明一个名为customers的变量。也许你想用customerList代替。

当你发现一个错误时,花点时间通读你的代码,并确切地理解发生了什么。错误消息通常非常清楚问题所在。