2012-05-29 33 views
0

停止我創建了由這三個類爲什麼我的WP仿真器上啓動畫面

1)

namespace BusinessLogic 
{ 
    public class Bottle : INotifyPropertyChanged 
    { 
     // Due to INotifyPropertyChanged interface 
     public event PropertyChangedEventHandler PropertyChanged; 

     // Proprietà Title 
     private string title; 
     public string Title 
     { 
      set 
      { 
       if (title != value) 
       { 
        title = value; 
        OnPropertyChanged("Title"); 
       } 
      } 
      get 
      { 
       return title; 
      } 
     } 
     // Proprietà PhotoFileName 
     private string photoFileName; 
     public string PhotoFileName 
     { 
      set 
      { 
       if (photoFileName != value) 
       { 
        photoFileName = value; 
        OnPropertyChanged("PhotoFileName"); 
       } 
      } 
      get 
      { 
       return photoFileName; 
      } 
     } 
    protected virtual void OnPropertyChanged(string propChanged) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propChanged)); 
    } 
    } 
} 

2)類組成的WP類庫BusinessLogic項目

namespace BusinessLogic 
{ 
    public class Bottles : INotifyPropertyChanged 
    { 
     // Due to INotifyPropertyChanged interface 
     public event PropertyChangedEventHandler PropertyChanged; 

     // Proprietà MainTitle 
     private string mainTitle; 
    public string MainTitle 
    { 
     set 
     { 
      if (mainTitle != value) 
      { 
       mainTitle = value; 
       OnPropertyChanged("MainTitle"); 
      } 
     } 
     get 
     { 
      return mainTitle; 
     } 
    } 

    // Proprietà bottles 
    private ObservableCollection<Bottle> bottleSet = new ObservableCollection<Bottle>(); 
    public ObservableCollection<Bottle> BottleSet 
    { 
     set 
     { 
      if (bottleSet != value) 
      { 
       bottleSet = value; 
       OnPropertyChanged("BottleSet"); 
      } 
     } 
     get { return bottleSet; } 
    } 

    protected virtual void OnPropertyChanged(string propChanged) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propChanged)); 
    } 

} 
} 

3) BottlesPresenter類

namespace BusinessLogic 
{ 
    public class BottlesPresenter : INotifyPropertyChanged 
    { 
    // Due to INotifyPropertyChanged interface 
    public event PropertyChangedEventHandler PropertyChanged; 


    // Proprietà BottleMatrix 
    private Bottles bottlesMatrix; 
    public Bottles BottlesMatrix 
    { 
     protected set 
     { 
      if (bottlesMatrix != value) 
      { 
       bottlesMatrix = value; 
       OnPropertyChanged("BottlesMatrix"); 
      } 
     } 
     get { return bottlesMatrix; } 
    } 

    public BottlesPresenter() 
    { 
     XmlSerializer xml = new XmlSerializer(typeof(Bottles)); 

     using (StreamReader fileReader = new   StreamReader(@"C:\Stuff\WindowsPhone\AppLearningHowTo2\AppLearningHowTo2\DAL\DB.xml")) 
     { 
      BottlesMatrix = (Bottles)xml.Deserialize(fileReader); 
     } 
    } 

     protected virtual void OnPropertyChanged(string propChanged) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propChanged)); 
     } 
    } 
} 

的BottlePresenter構造應該從位於到文件系統的XML文件反序列化。它包含以下標籤

<?xml version="1.0"?> 
<Bottles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <MainTitle>MainTitle</MainTitle> 
    <Bottleset> 
    <Bottle> 
     <Title>Title1</Title> 
     <PhotoFileName>PhotoFileName1</PhotoFileName> 
    </Bottle> 
    <Bottle> 
     <Title>Title2</Title> 
     <PhotoFileName>PhotoFileName2</PhotoFileName> 
    </Bottle> 
    </Bottleset> 
</Bottles> 

然後,我創建了一個WP應用,我作出了BusinessLogic.dll項目庫的引用。

在MainPage.xaml文件,我把XML命名空間聲明

xmlns:businesslogic="clr-namespace:BusinessLogic;assembly=BusinessLogic" 

我然後實例化MainPage.xaml中資源收集

<phone:PhoneApplicationPage.Resources> 
    <businesslogic:BottlesPresenter x:Key="bottlesPresenter" />   
</phone:PhoneApplicationPage.Resources> 

BottlesPresenter類,最後放一個TextBlock在與該資源綁定的內容區域中:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <TextBlock HorizontalAlignment="Center" 
     VerticalAlignment="Center" 
     Text="{Binding Source={StaticResource bottlesPresenter}, 
            Path=Bottles.MainTitle}" /> 

不幸的是,我啓動了調試器,模擬器開關打開,到達閃屏並且沒有繼續。

簡而言之:我無法創建一個BottlesPresenter類的實例。

我幾個星期沒有找到解決方案就把頭撞在牆上。

請有人能幫我一下嗎?

非常感謝您

安東尼

+0

任何調試輸出?你達到任何斷點?如果您刪除對BottlePresenter的引用,它是否至少會加載第一頁?你能發佈完整的repro嗎? –

+0

嗨馬特。沒有調試輸出和沒有達到斷點。刪除第一頁加載的引用沒有任何問題。 「發佈完整的repro」意味着什麼?我沒有找到上傳StackOverFlow整個解決方案的方法。無論如何,如果你給我你的電子郵件地址,我可以給你發送解決方案。許多很多很多謝謝。安東尼奧 – LarAnto

回答

0

仿真器的行爲就像當WP7不能構造應用程序對象。從問題來看,我只看到1個來自應用程序的引用到你的代碼中。它是資源中的BottlePresenter。 XamlLoader嘗試創建此類型的實例。

看到您BottlePresenter constructur內:

public BottlesPresenter() 
{ 
    XmlSerializer xml = new XmlSerializer(typeof(Bottles)); 

    using (StreamReader fileReader = new StreamReader(
    @"C:\Stuff\WindowsPhone\AppLearningHowTo2\AppLearningHowTo2\DAL\DB.xml")) 
    { 
     BottlesMatrix = (Bottles)xml.Deserialize(fileReader); 
    } 
} 

第一行確定。 第二行是OK。 第三行導致異常,因爲在Windows Phone上不接受路徑「C:\ Stuff \ WindowsPhone \ AppLearningHowTo2 \ AppLearningHowTo2 \ DAL \ DB.xml」。 您可以訪問的所有文件都是XAP的內容,程序集中的資源以及獨立存儲中的文件。

以下文章可以幫助你All about WP7 Isolated Storage - Read and Save Text files