c# - 将 View 绑定(bind)到 ViewModel 时 WPF 中的工具提示内存泄漏

标签 c# wpf mvvm memory-leaks tooltip

我在WPFMVVM 中有一个应用程序。在 ListBox 中显示 Images 列表,每个图像都关联到不同的 session 。

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

我的 View 模型

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

每当出现工具提示时,就会出现内存泄漏,但不明白为什么会发生, 我的问题是,在显示 ToolTip 之后,我是否需要使用 ToolTipdispose 任何内存?如果是这样该怎么做?

编辑

没有订阅任何事件。只绑定(bind)不同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 中显示没有任何 View 的工具提示给我同样的内存泄漏

这是我的 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”测试通过软件或硬件呈现的内容,并为软件呈现的元素启用紫色调。

希望对您有所帮助。

关于c# - 将 View 绑定(bind)到 ViewModel 时 WPF 中的工具提示内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32764764/

相关文章:

c# - 如何发送带有查询字符串的 URL 作为查询字符串

c# - 使用正则表达式的字符串到字典(想要优化)

c# - WPF事件捕获新添加的控件?

c# - Caliburn Micro VS MVVM Light 这些框架是一样的吗?

javascript - KnockoutJS 中的 ViewModel 无法访问全局变量

c# - Linq like for Java

c# - 如何让用户在 asp.net mvc 2 中异步下载多个文件?

.net - DataGridRow 上的 WPF DataGrid MouseOver

c# - OpacityMask 无法按预期在 C# 代码中工作

c# - 仅附加在第一个用户控件实例上的附加属性