im使用CodeProject中所述的类似方式使用Load / Save layoud。捕获LayoutSerializationCallback事件并尝试找到LayoutItem的核心响应viewModel
private void LayoutSerializer_LayoutSerializationCallback(object sender, LayoutSerializationCallbackEventArgs e)
{
// This can happen if the previous session was loading a file
// but was unable to initialize the view ...
if (string.IsNullOrWhiteSpace(e.Model.ContentId) || (e.Content = ReloadItem(e.Model)) == null)
{
e.Cancel = true;
return;
}
}
private object ReloadItem(object item)
{
object ret = null;
switch (item)
{
case LayoutAnchorable anchorable:
//list of tools windows
ret = Manager.Tools.FirstOrDefault(i => i.ContentId == anchorable.ContentId);
if(ret == null && anchorable.ContentId.StartsWith(MapPanel.MapPanelPrefix))
{
MapPanels.Add(anchorable);
}
break;
case LayoutDocument document:
// list of restored documents
ret = Manager.Documents.FirstOrDefault(i => i.ContentId == document.ContentId);
break;
default:
throw new NotImplementedException("Not implemented type of AD item ");
}
return ret;
}
当我在反序列化/还原布局时拥有所有可用的ViewModel时,此方法很好。
但是我正在考虑延迟布局还原之类的操作。就我而言,我在开始时有一些文件和面板。但是可能会有一些面板(称为MapPanel)在以后加载(viewModels在将来加载)。而且我不知道如何恢复这些面板的布局。
对于这种情况,我有List MapPanels来存储在avalondock布局加载时加载的可锚定对象,并尝试在ILayoutUpdateStrategy的BeforeInsertAnchorable中还原它们。但是当我调试它时,存储的LayoutAnchorable具有存储一个的不同父对象。因此,我认为在LayoutSerializationCallback中取消(e.Cancel = true)后,会以某种方式修改未还原的固定对象。
public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
{
if (anchorableToShow.Content is ToolPanel tool)
{
if(tool is MapPanel)
{
anchorableToShow = LayoutSaveLoadUtil.Instance.MapPanels.FirstOrDefault(mp => mp.ContentId == anchorableToShow.ContentId);
}
var destPane = destinationContainer as LayoutAnchorablePane;
if (destinationContainer != null && destinationContainer.FindParent<LayoutFloatingWindow>() != null)
return false;
var dockLeftPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == tool.PreferredLocation + "Pane");
if (dockLeftPane != null)
{
dockLeftPane.Children.Add(anchorableToShow);
return true;
}
return false;
}
return false;
}
太奇怪了,什么是实现此目标的正确方法。我还考虑过在加载MapPanel之后(再次)恢复布局,但我不知道如何跳过所有其他LayoutItems。那么,是否有可能恢复单个可锚位置,浮动父对象,停靠,大小等?]
所以我可能想出了解决方案。
我有一些面板(称为MapPanel),当从XML恢复布局时,不会加载它们的内容。就我而言,我有一个应用程序,其中包含一些文档和标签,在其他情况下,用户可以加载其他数据以显示地图。
而且我需要在用户加载地图时恢复此地图的布局。 (单击按钮,选择在哪里加载地图等)
当我具有LayoutAnchorable类型的附加列表MapPanelsStorage时,我有一个名为LayoutSaveLoadUtil的静态类(如代码项目所述)。在此列表中,我存储了在布局还原中缺少内容且ContentId具有特定前缀的所有布局。这告诉我是MapPanel。然后,我创建一个虚拟内容,将其分配给此面板并将其可见性设置为false(因此面板不可见)
(在layoutSerializationCallback中被称为)
private object ReloadItem(object item)
{
object ret = null;
switch (item)
{
case LayoutAnchorable anchorable:
//list of tools windows
ret = Manager.Tools.FirstOrDefault(i => i.ContentId == anchorable.ContentId);
if(ret == null && anchorable.ContentId.StartsWith(MapPanel.MapPanelPrefix))
{
//when layoutAnchorable is MapPanel (has MapPanel prefix) and its content is loaded yet
//store its layout into list to restore it later (when content is loaded)
MapPanelsStorage.Add(anchorable);
//Set anchorable visibility to false
anchorable.IsVisible = false;
//return dummy model for avalondock layout serialization
ret = new EmptyMapViewModel();
}
break;
case LayoutDocument document:
// list of restored documents
ret = Manager.Documents.FirstOrDefault(i => i.ContentId == document.ContentId);
break;
default:
throw new NotImplementedException("Not implemented type of AD item ");
}
return ret;
}
然后在BeforeInsertAnchorable中(将新面板添加到布局中时调用),我检查面板内容是否为MapPanel,查找存储的布局,尝试查找父项(LayoutAnchorablePane / LayoutDocumentPane),然后添加它而不是DummyHidden面板,然后将其删除从存储中。
//hacky hacky to restore map panel layout when map is opened after layout is loaded
//in layout deserialization, deserialize layout for MapPanels that hasnt DataModels yet with DummyModel to preserve their layout
if (anchorableToShow.Content is MapPanel mappanel)
{
var storedMapsLayout = LayoutSaveLoadUtil.Instance.MapPanelsStorage;
//check if Map panel has stored layout from previous layout deserialization
var matchingAnchorable = storedMapsLayout.FirstOrDefault(m => m.ContentId == mappanel.ContentId);
if (matchingAnchorable != null)
{
//make preserved layout visible, so its parent and etc is restored. Without this, correct parent LayoutGroup isnt found
matchingAnchorable.IsVisible = true;
LayoutAnchorablePane matchingAnchorablePane;
LayoutDocumentPane matchingDocumentPane;
//find parent layoutGroup. This can be LayoutAnchorablePane or LayoutDocumentPane
if ((matchingAnchorablePane = matchingAnchorable.FindParent<LayoutAnchorablePane>()) != null)
{
//add new panel into layoutGroup with correct layout
matchingAnchorablePane.Children.Add(anchorableToShow);
//remove old dummy panel
matchingAnchorablePane.RemoveChild(matchingAnchorable);
//remove restored layout from storage
storedMapsLayout.Remove(matchingAnchorable);
return true;
}
else if ((matchingDocumentPane = matchingAnchorable.FindParent<LayoutDocumentPane>()) != null)
{
//add new panel into layoutGroup with correct layout
matchingDocumentPane.Children.Add(anchorableToShow);
//remove old dummy panel
matchingDocumentPane.RemoveChild(matchingAnchorable);
//remove restored layout from storage
storedMapsLayout.Remove(matchingAnchorable);
return true;
}
else
{
matchingAnchorable.IsVisible = false;
}
}
}