将视图绑定到 ViewModel 时 WPF 中的工具提示内存泄漏

ToolTip Memory leak in WPF while binding a View to ViewModel

我在 WPFMVVM 有申请。在 ListBox 中显示 Images 的列表,每个图像都关联到不同的会话。

我的ListBoxItemTemplate是这样的,

<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>

我的视图模型

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

每当出现工具提示时,就会出现内存泄漏,但不明白为什么会发生, 我的问题是,在显示 ToolTip 之后,我与 ToolTipdispose 的任何记忆有什么关系吗?如果是这样该怎么做?

编辑

没有订阅任何事件。只绑定不同ViewModels不同ViewsDataContext

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>

这有点摸不着头脑,但可以尝试一下。我遇到过某些图形驱动程序会以某种方式触发 WPF 泄漏的问题。我们结束了 WPF 禁用硬件加速和重新测试。这不是一个理想的答案,但足以让我们在很长一段时间内为客户 运行 客户解决这个问题。

在注册表中(系统范围)

[HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics]
"DisableHWAcceleration"=dword:00000001

或通过应用程序本身的代码:

   RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;

您可以使用 "WPF Performance Suite" 测试通过软件或硬件呈现的内容,并为软件呈现的元素启用紫色色调。

希望对您有所帮助。

我已经通过创建一个模拟实际绑定的接口来修复它。见下文。

 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" />      

现在内存不会随着我们更改工具提示而增加。