2015-07-01 40 views
1

我使用AvalonDock,我會序列化和反序列化我的佈局。但回調(我使用ContentId創建正確對象的實例)並不總是被調用。出於這個原因,加載不能正常工作。我試圖添加一個「try-catch」,但沒有拋出異常。LayoutSerializationCallback不被調用

這是我的代碼:

`VAR layoutSerializer =新XmlLayoutSerializer(經理);

layoutSerializer.LayoutSerializationCallback += (s, e) => 
    { 
    MyClass item; 
    if (items.TryGetValue(e.Model.ContentId, out item)) 
    { 
     e.Content = item; 

     var tool = item as IMyClassToolApp; 
     var anchorable = e.Model as LayoutAnchorable; 

     var document = item as IMyClassDocumentApp; 
     var layoutDocument = e.Model as LayoutDocument; 

     if (tool != null && anchorable != null) 
     { 
     addToolCallback(tool); 
     tool.IsVisible = anchorable.IsVisible; 
     tool.IsSelected = e.Model.IsSelected; 
     return; 
     } 

     if (document != null && layoutDocument != null) 
     { 
     addDocumentCallback(document); 

     // Nasty hack to get around issue that occurs if documents are loaded from state, 
     // and more documents are opened programmatically. 
     layoutDocument.GetType().GetProperty("IsLastFocusedDocument").SetValue(layoutDocument, false, null); 

     document.IsVisible = true; 
     document.IsSelected = layoutDocument.IsSelected; 
     return; 
     } 
    } 

    e.Cancel = true; 
    }; 

    try 
    { 
    layoutSerializer.Deserialize(stream); 
    } 
    catch 
    { 
    return false; 
    }` 

謝謝你的幫忙!

回答

1

你確定你的流是O.K嗎?例如,如果配置文件不存在,則不會調用LayoutSerializationCallback方法。

private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
{ 
    try 
    { 
     MainWindow.logger.Debug("Entering: {0}", "MainWindow_Loaded"); 

     string filePath = Path.Combine(
      Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), 
      @"Jofta\Analyzer\configs\AvalonDock.config"); 

     if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) 
     { 
      XmlLayoutSerializer serializer = new XmlLayoutSerializer(this.dockingManager); 
      serializer.LayoutSerializationCallback += this.Serializer_LayoutSerializationCallback; 
      serializer.Deserialize(filePath); 
     } 

     MainWindow.logger.Debug("Exiting: {0}", "MainWindow_Loaded"); 
    } 
    catch (Exception ex) 
    { 
     MainWindow.logger.Error("Exception in: {0}", "MainWindow_Loaded"); 
     MainWindow.logger.Error("Message: {0}", ex.Message); 
    } 
} 



private void Serializer_LayoutSerializationCallback(object sender, LayoutSerializationCallbackEventArgs e) 
{ 
    try 
    { 
     MainWindow.logger.Debug("Entering: {0}", "serializer_LayoutSerializationCallback"); 

     if (e.Model.ContentId == ObjectExplorerViewModel.AnchorableContentId) 
     { 
      e.Content = Workspace.Instance.ObjectExplorer; 
      return; 
     } 

     // ... 
     // ... 

     MainWindow.logger.Debug("Exiting: {0}", "serializer_LayoutSerializationCallback"); 
    } 
    catch (Exception ex) 
    { 
     MainWindow.logger.Error("Exception in: {0}", "serializer_LayoutSerializationCallback"); 
     MainWindow.logger.Error("Message: {0}", ex.Message); 
    } 
} 
+0

我的文件沒問題,我一步一步地調試代碼,並且「Deserialize」沒有拋出異常。 –

+0

發佈更多詳細信息會更好。代碼的更多片段。例如配置文件的內容。讓所有的LayoutAnocorables和LayoutDocuments都是他們的ID? – Jofta

+0

我用我的代碼更新了這個問題... –