Prism RegionAdapter-删除然后添加视图 [英] Prism RegionAdapter - Removing then Adding View
问题描述
我有一个包含AvalonDock的棱镜/wpf/mef解决方案.我创建了一个RegionAdapterBase<Pane>
类,该类处理从AvalonDock创建和删除窗格.
I have a prism/wpf/mef solution that contains an AvalonDock. I created a RegionAdapterBase<Pane>
class that handles creating and removing the Panes from AvalonDock.
这是我遇到的问题:
- 我单击菜单中的一个按钮,并且一个视图已注册到一个区域并显示在我的
DocumentPane
中
- 我单击AvalonDock中的关闭按钮以关闭选项卡并删除视图
- 我单击同一菜单按钮再次将其重新添加
- 我收到错误:
- I click a button in my menu and a view is registered with a region and displayed in my
DocumentPane
- I click the close button in AvalonDock to close the tab and remove the view
- I click the same menu button to add it back again
- I receive the error:
指定的元素已经是 另一个元素的逻辑子元素. 首先断开连接."
"Specified element is already the logical child of another element. Disconnect it first."
所以...这告诉我需要删除一些东西,但是我无法弄清楚它在哪里.这是我的RegionAdapter中的一些代码:
So... this tells me that something is lingering that I need to remove, but I cannot figure out where it is. Heres some code from my RegionAdapter:
private void OnViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, IRegion region, Pane regionTarget)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (object item in e.NewItems)
{
UIElement view = item as UIElement;
if (view is ITabViewInfo)
{
if (view != null)
{
DockableContent newContentPane = new DockableContent()
{
Content = item,
Title = ((ITabViewInfo)view).TabViewTitle,
Icon = new Image()
{
Source = new BitmapImage(((ITabViewInfo)view).TabViewIcon)
}.Source,
IsCloseable = ((ITabViewInfo)view).IsCloseable,
HideOnClose = ((ITabViewInfo)view).IsHideOnClose
};
newContentPane.Closed += (contentPaneSender, args) =>
{
Debug.WriteLine("Removing view from region", "Prism");
region.Remove(item);
};
regionTarget.Items.Add(newContentPane);
newContentPane.Activate();
}
}
}
} else if (e.Action == NotifyCollectionChangedAction.Remove) {
regionTarget.Items.Clear();
}
}
在我的调试行中,DocumentPane
和区域视图已被正确破坏...当我单击将项目添加回视图时,在执行以下操作的行上收到上述错误消息:
From my debug lines, the DocumentPane
and region views are properly being destroyed... when I click to add the item back to the view, I get the above error message on the line that does:
Content = item,
这里是按下菜单按钮时运行的模块中的代码:
Heres the code from my module that runs when the menu button is pressed:
if (_regionManager.Regions["MainRegion"].Views.Any(m => m.GetType() == typeof(Views.ClassicFrontierView)))
{
Debug.WriteLine(_regionManager.Regions["MainRegion"].Views.Count());
}
else
{
Debug.WriteLine("Adding view to region", "Prism");
_regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.ClassicFrontierView));
}
知道我缺少什么吗?
推荐答案
我处理Closing事件,而不是处理Closed事件(可能会丢失对基础视图的引用).
Instead of handling the Closed event (which may have lost a reference to the underlying view), I handle the Closing event.
这可行,但是,当我尝试重新打开选项卡时,它显示的是同一实例.阅读此内容后,在复合WPF中 (Prism),IRegion.Add和IRegionManager.RegisterViewWithRegion有什么区别?我对此进行了更改:
This worked, however, when I tried to re-open the tab, it was displaying the same instance. After reading this In Composite WPF (Prism), what is the difference between IRegion.Add and IRegionManager.RegisterViewWithRegion? I changed this:
_regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.ClassicFrontierView));
对此:
_regionManager.Regions["MainRegion"].Add(new Classic.Views.ClassicFrontierView());
我仍然必须对Prism/avalondock进行一些研究,以确保不会出现内存泄漏,但到目前为止,它似乎仍在工作.
I still have to do some research with Prism / avalondock to make sure there will be no memory leaks, but as of now it appears to be working.
这篇关于Prism RegionAdapter-删除然后添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!