如何处理DataTemplate创建的用户控件 [英] How to dispose of usercontrols created by DataTemplate

查看:310
本文介绍了如何处理DataTemplate创建的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在WPF中的两个视图之间切换。我有一个DataTemplate,它使用ViewModel来推断根据ViewModel绘制的View。简而言之:

 < DataTemplate DataType ={x:Type ..:RedScreenViewModel}> 
< ...:RedScreenViewModel />
< / DataTemplate>

我决定在视图之间快速切换..和WPF应用程序的内存使用量高达2gb现在你可能会说现实生活中没有人会做我所做的事情。但是我想知道如何释放分配的内存。卸载绝对被称为,我已经取消订阅任何事件处理程序。
但是没有帮助。 DevExpress或WPF是否具有可以让.NET处理用户控件的属性?
我发现DevExpress但是做zilch是:

  DisposeOnWindowClosing 
DestroyOnClosingChildren

创建的视图非常复杂,我已经重组了布局以努力节省内存。但同样增加。
建议非常感谢。



编辑:
然而,析构函数不被调用...

解决方案

这是你如何处理嵌套在ItemsControl中的UserControl(在这种情况下是一个ListBox)

  public void Dispose()
{
if(this.listb!= null)
{
for(int count = 0; count< ; this.listb.Items.Count; count ++)
{
DependencyObject container = this.listb.ItemContainerGenerator.ContainerFromIndex(count);
UserControl userControl = container.GetVisualDescendent< UserControl>();
IDisposable controlToPotentiallyDispose = userControl为IDisposable;
if(controlToPotentiallyDispose!= null)
controlToPotentiallyDispose.Dispose();
controlToPotentiallyDispose = null;
}
}
if(this.ViewModel!= null)
{
this.ViewModel.Dispose();
this.ViewModel = null;
}
this.listb = null;
}

请注意,listb是从中查找项目的ListBox的x:Name。

此外,这个Dispose()方法应该在xaml.cs中,只要您不再需要该视图就应该调用。



HTH,



Bab。


I have to switch between two Views in WPF. I have a DataTemplate that uses ViewModels to infer which View to draw depending on the ViewModel. In short:

<DataTemplate DataType="{x:Type ..:RedScreenViewModel}">
<...:RedScreenViewModel/>
</DataTemplate>

On a whim I decided to switch rapidly between views.. and the memory usage of the WPF app shot up to 2gb. Now you may argue that in real life no one will ever do what I did. But I would like to know how to free the memory that was allocated. Unload is definitely called, I have unsubscribed from any event handlers. But it doesn't help. Does DevExpress or WPF have a property that can tell .NET to dispose of usercontrol? Ones I found for DevExpress but do zilch are:

DisposeOnWindowClosing
DestroyOnClosingChildren

The View that created are very complex, I have reorganized layout in efforts to save memory. But the same increase. Suggestions would be great thanks.

EDIT: The destructor isn't called however...

解决方案

This is how you dispose of UserControls nested inside an ItemsControl (in this case: a ListBox)

        public void Dispose()
        {
            if (this.listb != null)
            {
                for (int count = 0; count < this.listb.Items.Count; count++)
                {
                    DependencyObject container = this.listb.ItemContainerGenerator.ContainerFromIndex(count);
                    UserControl userControl = container.GetVisualDescendent<UserControl>();
                    IDisposable controlToPotentiallyDispose = userControl as IDisposable;
                    if (controlToPotentiallyDispose != null)
                        controlToPotentiallyDispose.Dispose();
                    controlToPotentiallyDispose = null;
                }
            }
            if (this.ViewModel != null)
            {
                this.ViewModel.Dispose();
                this.ViewModel = null;
            }
            this.listb = null;
        }

Note that listb is the x:Name of the ListBox to find items from.
Also, this Dispose() method should be in the xaml.cs and should be called whenever you don't need the view anymore.

HTH,

Bab.

这篇关于如何处理DataTemplate创建的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆