2015-06-03 23 views
-3

即時通訊使用c#和xaml創建windows dev。學習與搶劫英里「藍皮書」爲win8開發。這個代碼有什麼錯誤[windos phone 8.1 development] C#和Xaml

我不斷收到此錯誤消息,我卡住了。有人可以幫助我或給我一些指針。我在給我帶來麻煩的區塊添加了評論。 感謝

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> 
+3

什麼是錯誤信息? –

+6

消息非常清晰。你從來沒有定義一個名爲'customers'的變量。 – dcastro

+2

此外,「這是什麼錯誤的代碼[windos手機8.1開發] C#和Xaml」是一個*實際上*壞問題的標題。 – dcastro

回答

2

書籍可以容易出錯,直接複製的東西出了一本書可能不會幫助你學到很多東西,而不是嘗試新事物出來的擁有。如果書中有錯誤,那麼這本書是錯誤的,而不是編譯器。這本書不是法律,編譯器是。

至於你的實際問題,如dcastro指出,你還沒有聲明一個名爲customers的變量。也許你打算使用customerList來代替。

當您發現錯誤時,花一些時間仔細閱讀您的代碼並準確理解發生了什麼。錯誤消息通常在問題上很清楚。

+0

我已經試過,我自己,我知道我應該創造cusomers varialbe,但是當我確實創建customers變量的「customersStackPanel.Children ...」對象也會把「customersStackPanel」不存在儘管它清楚地表明它從xaml中被引用。我剛丟了 – emekaokoli

+0

** customersStackPanel **是你的'View'的一個元素。你不能在你的類中引用它,你只能在視圖的代碼隱藏中引用它。 –