首页 新闻 赞助 找找看

wpf 内存释放 关于listbox 和 visualbrush

0
悬赏园豆:200 [已解决问题] 解决于 2015-12-16 16:46

我用wpf实现了一个多页的画板程序。

画板的每一页都是我自实现的page控件。

在画板中,为了界面友好和便捷的页间跳转,我又实现了一个页导航栏控件,其中包含一个listbox,我将listbox的itemsrouce属性设置为画板中的page列表,listboxitem则通过visualbrush实时显示各页的内容。

可是,现在发现,当我关了画板窗口(程序没有完全关闭,还有其他功能在运行),并释放了导航栏之后,被导航栏吃掉的内存确一点也没释放。我可是专门实现并调用了导航栏中的dispose方法,其中清空了itemsrouce和listbox的各种style。(导航栏的dispose方法如下)

private bool _isDisposed = false;

public void Dispose()
{
if (!_isDisposed)
{
_isDisposed = true;

this.PagesList.ItemsSource = null;
this.PagesList.SelectedItem = null;
this.PagesList.ClearValue(ListBox.ItemsSourceProperty);
this.PagesList.ClearValue(ListBox.StyleProperty);
this.PagesList.ClearValue(ListBox.ItemContainerStyleProperty);
this.PagesList.ClearValue(ListBox.ItemTemplateProperty);

this.RootPanel.Children.Clear();
this.Resources.Clear();

ClearValue(Navigator.DataContextProperty);
ClearValue(Navigator.ContentProperty);
ClearValue(Navigator.RenderTransformProperty);
}
}

 注:PagesList是导航中栏中的listbox控件

  this则是导航栏控件,导航栏是派生自usercontrol,因此有一个content属性 

请问,如何才能释放listboxitem中的visualbrush占用的内存。  

问题补充:
        private bool _isDisposed = false;

        public void Dispose()
        {
            if (!_isDisposed)
            {
                _isDisposed = true;

                this.PagesList.ItemsSource = null;
                this.PagesList.SelectedItem = null;
                this.PagesList.ClearValue(ListBox.ItemsSourceProperty);
                this.PagesList.ClearValue(ListBox.StyleProperty);
                this.PagesList.ClearValue(ListBox.ItemContainerStyleProperty);
                this.PagesList.ClearValue(ListBox.ItemTemplateProperty);

                this.RootPanel.Children.Clear();
                this.Resources.Clear();

                ClearValue(Navigator.DataContextProperty);
                ClearValue(Navigator.ContentProperty);
                ClearValue(Navigator.RenderTransformProperty);
            }
        }
madgecko的主页 madgecko | 初学一级 | 园豆:6
提问于:2015-12-01 16:29
< >
分享
最佳答案
1

.net程序内存释放没有那么实时的,如果确保visualbrush没有被引用的话,调一下GC.Collect强制回收试试,

收获园豆:200
天方 | 大侠五级 |园豆:5407 | 2015-12-01 23:58

visualbrush是在itemtemplate中定义的,而itemsrouce则是通过绑定实现的

在释放前使用一个bindingoptions.clearallbindings,效果就有了,虽然内存也不是实时释放,但还是可以回收的

madgecko | 园豆:6 (初学一级) | 2015-12-16 16:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册