将View绑定到ViewModel时WPF中的ToolTip内存泄漏 [英] ToolTip Memory leak in WPF while binding a View to ViewModel

查看:127
本文介绍了将View绑定到ViewModel时WPF中的ToolTip内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPFMVVM中有一个应用程序.在每个图像与不同会话相关联的ListBox中显示Images的列表.

我的ListBox ItemTemplate看起来像这样,

<ListBox.ItemTemplate>
   <DataTemplate>
      <Image 
          Source="{Binding IsClaims,Converter={StaticResource PolicyClaimsImageSelector}}" 
          ToolTipService.ShowDuration="7000">
             <Image.ToolTip>                                           
                <StackPanel>
                  <TextBlock Text="{Binding WorkingSessionName}" />
                  <Views:ToolTipView DataContext="{Binding ThisViewModel}"/>
                </StackPanel>
            </Image.ToolTip>
      </Image>
   </DataTemplate>
</ListBox.ItemTemplate>

我的ViewModel

public class Session : BindableBase
{
    private MainViewModel _ThisViewModel;
    public MainViewModel ThisViewModel
    {
        get
        {
            return _ThisViewModel;
        }
        set
        {
            _ThisViewModel = value;
            NotifyPropertyChanged();
        }
    }
}

每当工具提示出现时,就会出现内存泄漏,不知道为什么会发生, 我的问题是,在显示ToolTip之后,我与ToolTipdispose有什么关系?如果可以的话该怎么做?

编辑

没有事件订阅.仅绑定不同Views

的不同ViewModelsDataContext

ToolTipView.XAML

 <DockPanel>
    <xcad:DockingManager  DockPanel.Dock="Left" Grid.Row="2" BorderBrush="Black" BorderThickness="1">
        <xcad:DockingManager.Theme>
            <xcad:MetroTheme />
        </xcad:DockingManager.Theme>
        <xcad:LayoutRoot >
            <xcad:LayoutPanel Orientation="Horizontal" >
                <xcad:LayoutAnchorablePaneGroup Orientation="Horizontal" DockMinWidth="150" >
                    <xcad:LayoutAnchorablePane >
                        <xcad:LayoutAnchorable Title="Folder" x:Name="ExplorerView" AutoHideWidth="300">
                            <Views:ExplorerView DataContext="{Binding ExplorerViewModel}"/>
                        </xcad:LayoutAnchorable>
                    </xcad:LayoutAnchorablePane>
                </xcad:LayoutAnchorablePaneGroup>
                <xcad:LayoutAnchorablePaneGroup Orientation="Horizontal" DockMinWidth="450" >
                    <xcad:LayoutAnchorablePane >
                        <xcad:LayoutAnchorable Title="Documents"  x:Name="TOC">
                            <Views:TableOfContentView  DataContext="{Binding TableOfContentViewModel}"/>
                        </xcad:LayoutAnchorable>
                    </xcad:LayoutAnchorablePane>
                </xcad:LayoutAnchorablePaneGroup>
                <xcad:LayoutAnchorablePaneGroup Orientation="Vertical" DockMinWidth="320">
                    <xcad:LayoutAnchorablePane DockMinHeight="400" >
                        <xcad:LayoutAnchorable Title="Properties"  x:Name="Property">
                            <Views:PropertyView DataContext="{Binding PropertyViewModel}"/>
                        </xcad:LayoutAnchorable>
                    </xcad:LayoutAnchorablePane>
                    <xcad:LayoutAnchorablePane >
                        <xcad:LayoutAnchorable Title="Search"  x:Name="Search">
                            <Views:SearchPanel DataContext="{Binding SearchViewModel}"/>
                        </xcad:LayoutAnchorable>
                    </xcad:LayoutAnchorablePane>
                </xcad:LayoutAnchorablePaneGroup>                    
            </xcad:LayoutPanel>
        </xcad:LayoutRoot>
    </xcad:DockingManager>
</DockPanel>

编辑

我曾尝试从ToolTipView.XAML中删除所有Views,如下所示,并且显示的工具提示中没有ToolTip中的任何视图,这给了我同样的memory leak.

这是我的ToolTipView.XAML现在的样子,

<UserControl x:Class="ecclient.viewer.Views.ToolTipView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:Views="clr-namespace:ecclient.viewer.Views">
</UserControl>

解决方案

我已通过创建模拟实际绑定的接口来解决此问题.见下文.

 public IExplorer ToolTipExplorerViewModel
    {
        get { return ExplorerViewModel; }            
    }

    public ITableOfContents ToolTipTableOFContents
    {
        get { return TableOfContentViewModel; }
    }

现在我将这个属性与ToolTipView绑定在一起,即:

<Views:ExplorerView DataContext="{Binding ToolTipExplorerViewModel}" Grid.Column="0"/>
<Views:TableOfContentView  DataContext="{Binding ToolTipTableOFContents}" Grid.Column="1" />      

现在,随着我们更改工具提示,内存不会增长.

I have an application in WPF, MVVM. Shows a list of Images in a ListBox each image is associated to different session.

My ListBox ItemTemplate looks like this,

<ListBox.ItemTemplate>
   <DataTemplate>
      <Image 
          Source="{Binding IsClaims,Converter={StaticResource PolicyClaimsImageSelector}}" 
          ToolTipService.ShowDuration="7000">
             <Image.ToolTip>                                           
                <StackPanel>
                  <TextBlock Text="{Binding WorkingSessionName}" />
                  <Views:ToolTipView DataContext="{Binding ThisViewModel}"/>
                </StackPanel>
            </Image.ToolTip>
      </Image>
   </DataTemplate>
</ListBox.ItemTemplate>

My ViewModel

public class Session : BindableBase
{
    private MainViewModel _ThisViewModel;
    public MainViewModel ThisViewModel
    {
        get
        {
            return _ThisViewModel;
        }
        set
        {
            _ThisViewModel = value;
            NotifyPropertyChanged();
        }
    }
}

When ever the tool tip shows up there is a memory leak, not understanding why its happening, My question is that is there anything i have to do with ToolTip to dispose any memory after showing the ToolTip ?. if so how to do that ?

Edit

No Events are subscribed. Only binding the DataContext of different ViewModels of different Views

ToolTipView.XAML

 <DockPanel>
    <xcad:DockingManager  DockPanel.Dock="Left" Grid.Row="2" BorderBrush="Black" BorderThickness="1">
        <xcad:DockingManager.Theme>
            <xcad:MetroTheme />
        </xcad:DockingManager.Theme>
        <xcad:LayoutRoot >
            <xcad:LayoutPanel Orientation="Horizontal" >
                <xcad:LayoutAnchorablePaneGroup Orientation="Horizontal" DockMinWidth="150" >
                    <xcad:LayoutAnchorablePane >
                        <xcad:LayoutAnchorable Title="Folder" x:Name="ExplorerView" AutoHideWidth="300">
                            <Views:ExplorerView DataContext="{Binding ExplorerViewModel}"/>
                        </xcad:LayoutAnchorable>
                    </xcad:LayoutAnchorablePane>
                </xcad:LayoutAnchorablePaneGroup>
                <xcad:LayoutAnchorablePaneGroup Orientation="Horizontal" DockMinWidth="450" >
                    <xcad:LayoutAnchorablePane >
                        <xcad:LayoutAnchorable Title="Documents"  x:Name="TOC">
                            <Views:TableOfContentView  DataContext="{Binding TableOfContentViewModel}"/>
                        </xcad:LayoutAnchorable>
                    </xcad:LayoutAnchorablePane>
                </xcad:LayoutAnchorablePaneGroup>
                <xcad:LayoutAnchorablePaneGroup Orientation="Vertical" DockMinWidth="320">
                    <xcad:LayoutAnchorablePane DockMinHeight="400" >
                        <xcad:LayoutAnchorable Title="Properties"  x:Name="Property">
                            <Views:PropertyView DataContext="{Binding PropertyViewModel}"/>
                        </xcad:LayoutAnchorable>
                    </xcad:LayoutAnchorablePane>
                    <xcad:LayoutAnchorablePane >
                        <xcad:LayoutAnchorable Title="Search"  x:Name="Search">
                            <Views:SearchPanel DataContext="{Binding SearchViewModel}"/>
                        </xcad:LayoutAnchorable>
                    </xcad:LayoutAnchorablePane>
                </xcad:LayoutAnchorablePaneGroup>                    
            </xcad:LayoutPanel>
        </xcad:LayoutRoot>
    </xcad:DockingManager>
</DockPanel>

EDIT

I have tried removing all the Views from the ToolTipView.XAML like below and show the tool tip with out any View in the ToolTip gives me the same memory leak.

Here is what now my ToolTipView.XAML looks like,

<UserControl x:Class="ecclient.viewer.Views.ToolTipView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:Views="clr-namespace:ecclient.viewer.Views">
</UserControl>

解决方案

I have fixed it by creating an Interface to mimic the actual bindings. see below.

 public IExplorer ToolTipExplorerViewModel
    {
        get { return ExplorerViewModel; }            
    }

    public ITableOfContents ToolTipTableOFContents
    {
        get { return TableOfContentViewModel; }
    }

now i bind this properties with ToolTipView ie:

<Views:ExplorerView DataContext="{Binding ToolTipExplorerViewModel}" Grid.Column="0"/>
<Views:TableOfContentView  DataContext="{Binding ToolTipTableOFContents}" Grid.Column="1" />      

Now Memory is not growing as we change the tooltip.

这篇关于将View绑定到ViewModel时WPF中的ToolTip内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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