我有一个应用程序,它在ContentControl中一次显示一个视图。我有一个当前的解决方案,但很好奇是否有一个更好的内存管理。
我的当前设计在需要显示时创建新对象,并在它们不再可见时销毁它们。我很好奇这是否是更好的方法,或者保持对每个视图的引用并在这些引用之间交换更好?
以下是对我的应用程序布局的更多解释:
我的MainWindow.xaml的一个非常简化的版本如下所示:
<Window ... >
<Window.Resources>
<DataTemplate DataType="{x:Type vm:SplashViewModel}">
<view:SplashView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:MediaPlayerViewModel}">
<view:MediaPlayerView />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding ActiveModule}" />
</Grid>
</Window>
在我的MainViewModel.cs中,我将ActiveModule参数与新初始化的ViewModels交换。例如,我对交换内容的伪代码逻辑检查将类似于:
if (logicCheck == "SlideShow")
ActiveModule = new SlideShowViewModel();
else if (logicCheck == "MediaPlayer")
ActiveModule = new MediaPlayerViewModel();
else
ActiveModule = new SplashScreenViewModel();
但是,只是维持一个参考更适合速度和内存使用吗?
Alt Option 1:为每个ViewModel创建静态引用并在它们之间进行交换......
private static ViewModelBase _slideShow = new SlideShowViewModel();
private static ViewModelBase _mediaPlayer = new MediaPlayerViewModel();
private static ViewModelBase _splashView = new SplashScreenViewModel();
private void SwitchModule(string logicCheck) {
if (logicCheck == "SlideShow")
ActiveModule = _slideShow;
else if (logicCheck == "MediaPlayer")
ActiveModule = _mediaPlayer;
else
ActiveModule = _splashView;
}
我不是经常在这里创建/销毁,但这种方法在我看来是浪费了内存,未使用的模块只是挂了出去。或者...... WPF在幕后做了什么特别的事情来避免这种情况?
Alt Option 2:将每个可用模块放在XAML中并在那里显示/隐藏它们:
<Window ... >
<Grid>
<view:SplashScreenView Visibility="Visible" />
<view:MediaPlayerView Visibility="Collapsed" />
<view:SlideShowView Visibility="Collapsed" />
</Grid>
</Window>
同样,我很好奇在我不熟悉的后台可能会发生什么内存管理。当我崩溃的东西,它是否完全陷入了一种冬眠?我已经读过一些东西(没有测试,事件,关键输入,焦点......)但是动画和其他内容呢?
感谢您的任何意见!
答案 0 :(得分:3)
一旦我的视图创建起来非常昂贵,我遇到了这种情况,因此我想将它们存储在内存中,以避免在用户来回切换时重新创建它们。
我的最终解决方案是重用我用来完成相同行为的扩展TabControl
(停止WPF在切换选项卡时销毁TabItems),在切换选项卡时存储ContentPresenter
并重新加载如果可能,当你切换回来时。
我唯一需要改变的是我必须覆盖TabControl.Template
所以唯一显示的是TabControl的实际SelectedItem
部分
我的XAML最终看起来像这样:
<local:TabControlEx ItemsSource="{Binding AvailableModules}"
SelectedItem="{Binding ActiveModule}"
Template="{StaticResource BlankTabControlTemplate}" />
并且扩展TabControl
的实际代码如下所示:
// Extended TabControl which saves the displayed item so you don't get the performance hit of
// unloading and reloading the VisualTree when switching tabs
// Obtained from http://www.pluralsight-training.net/community/blogs/eburke/archive/2009/04/30/keeping-the-wpf-tab-control-from-destroying-its-children.aspx
// and made a some modifications so it reuses a TabItem's ContentPresenter when doing drag/drop operations
[TemplatePart(Name = "PART_ItemsHolder", Type = typeof(Panel))]
public class TabControlEx : System.Windows.Controls.TabControl
{
// Holds all items, but only marks the current tab's item as visible
private Panel _itemsHolder = null;
// Temporaily holds deleted item in case this was a drag/drop operation
private object _deletedObject = null;
public TabControlEx()
: base()
{
// this is necessary so that we get the initial databound selected item
this.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
}
/// <summary>
/// if containers are done, generate the selected item
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
if (this.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
{
this.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
UpdateSelectedItem();
}
}
/// <summary>
/// get the ItemsHolder and generate any children
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_itemsHolder = GetTemplateChild("PART_ItemsHolder") as Panel;
UpdateSelectedItem();
}
/// <summary>
/// when the items change we remove any generated panel children and add any new ones as necessary
/// </summary>
/// <param name="e"></param>
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
if (_itemsHolder == null)
{
return;
}
switch (e.Action)
{
case NotifyCollectionChangedAction.Reset:
_itemsHolder.Children.Clear();
if (base.Items.Count > 0)
{
base.SelectedItem = base.Items[0];
UpdateSelectedItem();
}
break;
case NotifyCollectionChangedAction.Add:
case NotifyCollectionChangedAction.Remove:
// Search for recently deleted items caused by a Drag/Drop operation
if (e.NewItems != null && _deletedObject != null)
{
foreach (var item in e.NewItems)
{
if (_deletedObject == item)
{
// If the new item is the same as the recently deleted one (i.e. a drag/drop event)
// then cancel the deletion and reuse the ContentPresenter so it doesn't have to be
// redrawn. We do need to link the presenter to the new item though (using the Tag)
ContentPresenter cp = FindChildContentPresenter(_deletedObject);
if (cp != null)
{
int index = _itemsHolder.Children.IndexOf(cp);
(_itemsHolder.Children[index] as ContentPresenter).Tag =
(item is TabItem) ? item : (this.ItemContainerGenerator.ContainerFromItem(item));
}
_deletedObject = null;
}
}
}
if (e.OldItems != null)
{
foreach (var item in e.OldItems)
{
_deletedObject = item;
// We want to run this at a slightly later priority in case this
// is a drag/drop operation so that we can reuse the template
this.Dispatcher.BeginInvoke(DispatcherPriority.DataBind,
new Action(delegate()
{
if (_deletedObject != null)
{
ContentPresenter cp = FindChildContentPresenter(_deletedObject);
if (cp != null)
{
this._itemsHolder.Children.Remove(cp);
}
}
}
));
}
}
UpdateSelectedItem();
break;
case NotifyCollectionChangedAction.Replace:
throw new NotImplementedException("Replace not implemented yet");
}
}
/// <summary>
/// update the visible child in the ItemsHolder
/// </summary>
/// <param name="e"></param>
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
base.OnSelectionChanged(e);
UpdateSelectedItem();
}
/// <summary>
/// generate a ContentPresenter for the selected item
/// </summary>
void UpdateSelectedItem()
{
if (_itemsHolder == null)
{
return;
}
// generate a ContentPresenter if necessary
TabItem item = GetSelectedTabItem();
if (item != null)
{
CreateChildContentPresenter(item);
}
// show the right child
foreach (ContentPresenter child in _itemsHolder.Children)
{
child.Visibility = ((child.Tag as TabItem).IsSelected) ? Visibility.Visible : Visibility.Collapsed;
}
}
/// <summary>
/// create the child ContentPresenter for the given item (could be data or a TabItem)
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
ContentPresenter CreateChildContentPresenter(object item)
{
if (item == null)
{
return null;
}
ContentPresenter cp = FindChildContentPresenter(item);
if (cp != null)
{
return cp;
}
// the actual child to be added. cp.Tag is a reference to the TabItem
cp = new ContentPresenter();
cp.Content = (item is TabItem) ? (item as TabItem).Content : item;
cp.ContentTemplate = this.SelectedContentTemplate;
cp.ContentTemplateSelector = this.SelectedContentTemplateSelector;
cp.ContentStringFormat = this.SelectedContentStringFormat;
cp.Visibility = Visibility.Collapsed;
cp.Tag = (item is TabItem) ? item : (this.ItemContainerGenerator.ContainerFromItem(item));
_itemsHolder.Children.Add(cp);
return cp;
}
/// <summary>
/// Find the CP for the given object. data could be a TabItem or a piece of data
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
ContentPresenter FindChildContentPresenter(object data)
{
if (data is TabItem)
{
data = (data as TabItem).Content;
}
if (data == null)
{
return null;
}
if (_itemsHolder == null)
{
return null;
}
foreach (ContentPresenter cp in _itemsHolder.Children)
{
if (cp.Content == data)
{
return cp;
}
}
return null;
}
/// <summary>
/// copied from TabControl; wish it were protected in that class instead of private
/// </summary>
/// <returns></returns>
protected TabItem GetSelectedTabItem()
{
object selectedItem = base.SelectedItem;
if (selectedItem == null)
{
return null;
}
if (_deletedObject == selectedItem)
{
}
TabItem item = selectedItem as TabItem;
if (item == null)
{
item = base.ItemContainerGenerator.ContainerFromIndex(base.SelectedIndex) as TabItem;
}
return item;
}
}
此外我并不积极,但我认为我的空白TabControl模板看起来像这样:
<Style x:Key="BlankTabControlTemplate" TargetType="{x:Type local:TabControlEx}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TabControlEx}">
<DockPanel>
<!-- This is needed to draw TabControls with Bound items -->
<StackPanel IsItemsHost="True" Height="0" Width="0" />
<Grid x:Name="PART_ItemsHolder" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 1 :(得分:1)
您可以选择继续使用当前的方法;提供:
保持视图模型缓存在内存中的一个缺点是它们的绑定。如果在视图超出范围时必须停止绑定通知以在视图和视图模型之间流动,则将viewmodel设置为null。如果viewmodel构造是轻量级的,则可以快速构造视图模型并分配回视图datacontext。
然后您可以按方法2中所示缓存视图。我相信如果使用适当的数据插入视图模型,则无法重复构建视图。当您将viewmodel设置为null时,由于绑定datacontext视图将获得所有绑定cleanedup。稍后将新的viewmodel设置为datacontext时,视图将加载新数据。
注意:确保在没有内存泄漏的情况下正确处理视图模型。使用SOS.DLL通过visual studio调试来检查viewmodel实例计数。
答案 2 :(得分:1)
另一个需要考虑的选择:这是使用IoC容器和依赖注入框架之类的方案吗? DI框架通常支持对象的容器管理生命周期。我建议看看Unity应用程序块或MEF是否符合您的想法。