2012-02-25 19 views
1

解析外部XAML文件時出現了一個非常奇怪的問題。預先的歷史是我想加載一個外部XAML文件,其中包含要處理的內容。但是我想加載儘可能多的不同文件。這是通過卸載舊的和加載新的。XamlReader.Load()的奇怪行爲?

我的問題是: 當我第一次加載xaml時,一切都很好,一切都應該如此。 但是,當我第二次加載相同的xaml時,im Loading的對象的每個條目都有兩次。如果我再運行一次,每個對象都有三次,依此類推...

要自己調試項目,請下載它here。該函數從文件「Control Panel.xaml.cs」的第137行開始。我真的不知道這是什麼。這是我的錯還是僅僅是一個錯誤?如果是,是否有解決方法?

/// <summary> 
    /// Load a xaml file and parse it 
    /// </summary> 
    public void LoadPresentation() 
    { 
     this.Title = "Control Panel - " + System.IO.Path.GetFileName(global.file); 

     System.IO.FileStream XAML_file = new System.IO.FileStream(global.file, System.IO.FileMode.Open); 
     try 
     { 
      System.IO.StreamReader reader = new System.IO.StreamReader(XAML_file); 
      string dump = reader.ReadToEnd(); //This is only for debugging purposes because of the strange issue... 
      XAML_file.Seek(0, System.IO.SeekOrigin.Begin); 

      presentation = (ResourceDictionary)XamlReader.Load(XAML_file); 

      //Keys the resourceDictionary must have to be valid 
      if (presentation["INDEX"] == null || presentation["MAIN_GRID"] == null || presentation["CONTAINER"] == null || presentation["LAYOUTLIST"] == null) 
      { 
       throw new Exception(); 
      } 

      //When this list is loaded, every item in it is there twice or three times or four... Why???? 
      TopicList Index = null; 
      Index = (TopicList)presentation["INDEX"]; 

      for (int i = 0; i < topics.Count;) 
      { 
       topics.RemoveAt(i); 
      } 

      foreach (TopicListItem item in Index.Topics) 
      { 
       topics.Insert(item.TopicIndex, (Topic)presentation[item.ResourceKey]); 
      } 

      lv_topics.SelectedIndex = 0; 
      selectedIndex = 0; 
     } 
     catch 
     { 
      System.Windows.Forms.MessageBox.Show("Failed to load XAML file \"" + global.file + "\"", "Parsing Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); 
      presentation = null; 
     } 
     finally 
     { 
      XAML_file.Close(); 
     } 
    } 

編輯:

我試圖序列化從XamlReader和輸出讀取無處任何元素childElement對象......但是,如果我拉出來的對象字典,孩子們都在那裏(重複和三重,但在那裏)。

我已經嘗試過

topics.Clear(); 

topics=new ObservableCollection<TopicListItem>(); 
lv_topics.ItemsSource=topics; 

回答

1

嘗試Index.Topics.Clear()Topics加載到您的主題對象後。似乎擺脫了重複。

//When this list is loaded, every item in it is there twice or three times or four... Why???? 
TopicList Index = null; 
Index = (TopicList)presentation["INDEX"]; 

topics.Clear(); 

foreach (TopicListItem item in Index.Topics) 
{ 
    topics.Insert(item.TopicIndex, (Topic)presentation[item.ResourceKey]); 
} 

Index.Topics.Clear();  //Adding this will prevent the duplication 
lv_topics.SelectedIndex = 0; 
selectedIndex = 0; 
+0

是的,解決了它,謝謝。但爲什麼?我不明白爲什麼這些對象連接在一起... – CShark 2012-02-26 12:09:26

+0

要回答你,我不知道爲什麼。我用你的代碼玩了大約一個小時,以確定我做了什麼。我將XamlReader.Load取消了,它引入了正確數量的主題,我注意到當您將演示文稿投射到TopicList時,會出現額外的主題。我猜測它與你的TopicList類型中的靜態聲明有關。你將它設置爲null然後重新分配它,我相信它會將舊數據連接起來。 – 2012-02-26 19:14:24

+0

但是不是一個靜態的DependencyProperty處理綁定的常用方法?如果是這樣,那麼應該有首先顯示的話題不應該嗎?我向其他幾個人展示了這個錯誤,他們的最終觀點是,.NET Framework中存在一個錯誤。我會嘗試重現這一點並收集更多信息...... – CShark 2012-02-27 16:22:18

0

在代碼後主題清除列表中沒有LoadPresentation聲明(),所以自然就會有任何先前值。

我知道你說你嘗試過topic = new ObservableCollection();但請再試一次。也放到了LoadPresentation()

public void LoadPresentation() 
    { 
     ObservableCollection<TopicListItem> topics = new ObservableCollection<TopicListItem>() 

我會通過文件名

public void LoadPresentation(string fileName) 

我得到你可能需要使用外LoadPresentation主題但這調試。如果您需要回報以外的話題。

public ObservableCollection<TopicListItem> LoadPresentation(string fileName) 

如果這樣不能解決問題,我會在XAML_file.Close()上放一個try catch塊。看看有什麼奇怪的事情沒有進行。

+0

如果您已經閱讀帖子到最後,您應該已經認識到我已經嘗試使用其他常用方法清除列表。 – CShark 2012-02-26 12:13:57

+0

顯然你沒有用盡所有的選項來清除,因爲你還沒有嘗試過Index.Topics.Clear()。根據你的文章,你還沒有嘗試在LoadPresentation()中聲明主題。 – Paparazzi 2012-02-26 17:31:53

+0

好的。現在我知道你在做什麼:清除Index.Topics。對不起,我不明白。 – CShark 2012-02-26 17:41:15