- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用此代码,您可以 100% 地重现我的问题。我创建了一个“父”ViewModel:
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.ObjectModel;
namespace WpfApplication1 {
public class ViewModel : BindableBase {
public ObservableCollection<ChildViewModel> Items { get; set; }
public DelegateCommand<ChildViewModel> CloseItem { get; set; }
public ViewModel() {
CloseItem = new DelegateCommand<ChildViewModel>(OnItemClose);
Items = new ObservableCollection<ChildViewModel>(new[] {
new ChildViewModel { Name = "asdf" },
new ChildViewModel { Name = "zxcv" },
new ChildViewModel { Name = "qwer" },
new ChildViewModel { Name = "fdgz" },
new ChildViewModel { Name = "hgjkghk" }
});
}
private void OnItemClose(ChildViewModel obj) {
Items.Remove(obj);
}
}
}
它包含 ChildViewModel
实例的 ObservableCollection
:
using Prism.Mvvm;
namespace WpfApplication1 {
public class ChildViewModel : BindableBase {
public string Name { get; set; }
protected byte[] leakArr = new byte[1024 * 1024 * 10];
}
}
如您所见,我在每个 ChildViewModel
中声明了一个 10mb 的数组。现在 View :
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
DataContext = new ViewModel();
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<TabControl ItemsSource="{Binding Items}">
<TabControl.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding Path=Name}" DockPanel.Dock="Left" Margin="2,3,0,2" />
<Button DockPanel.Dock="Right" BorderThickness="0" Background="Transparent"
Margin="10,2,2,2"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext.CloseItem}"
CommandParameter="{Binding}">
<Button.Content>
<TextBlock FontWeight="Bold" Text="X" />
</Button.Content>
</Button>
</DockPanel>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
</Window>
查看 VS2015 内存诊断工具,我可以看到即使每个 ChildViewModel
都已从 Items
集合中删除,它仍然存在于内存中,只有一个对象(对于每个虚拟机)仍然持有对它的引用 - Button
控件。有趣的是,如果我将 Button
的命令声明替换为:
Click="Button_Click"
private void Button_Click(object sender, RoutedEventArgs e) {
(DataContext as ViewModel).CloseItem.Execute(((sender as Button).DataContext as ChildViewModel));
}
没有内存泄漏 - 这意味着调试工具没有显示任何已处理的 ChildViewModel
实例。我不知道这是 Prism 特有的问题,还是 wpf 的某种怪癖。
我正在使用最新版本的 .net 和 prism 库。
最佳答案
我似乎找到了这个问题的解决方案(至少在我的应用程序中)——我停止直接绑定(bind)到 ChildViewModel
实例,而是绑定(bind) CommandParameter
到 ChildViewModel
的 Name
属性。然后,在 ViewModel
中,我只是浏览 Items 集合并删除具有匹配属性值的项目。现在 VS 诊断工具没有显示任何应该被 GC 而没有的对象,因此内存泄漏消失了。
Prism 团队正在分析这个问题:https://github.com/PrismLibrary/Prism/issues/345
关于c# - Prism 的 DelegateCommand 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34188671/
我想将使用 Prism 4 编写的项目迁移到 Prism 6。 在 Prism 4 中 CompositePresentationEvent类型位于Microsoft.Practices.Compos
Pattern of pub-sub events is that the publisher should not know or care if there are any subscribers
我刚刚使用 MVVMLight、PRISM 和 DryIoc 启动了我的第一个 WPF 应用程序。 App.xaml 引用 prism:PrismApplication,如 https://prism
Microsoft 的 Patterns and Practices 提供的示例非常有用: 大约六个更简单的快速入门 其中涉及具体问题 股票交易者引用实现 ,这是一个相当全面的应用程序 但它缺乏更有用
prism 中共享服务的目的和用途是什么? 哪些事情会让我认为我必须使用共享服务而不是 Event Aggregator? 最佳答案 从事件订阅者的角度来看 EventAggregator,它有利于获
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 3年前关闭。 Improve this qu
寻找一个简单的 helloWorld EventAggregator 示例。我试图理解这一点,并且在遵循 RI 示例时遇到了一些困难。 谢谢 N 最佳答案 Prism 4.0 包括 EventAggr
我只是想知道区域的意义是什么。我想我不明白他们解决的问题。 例如,我看到很多人使用区域作为导航区域,但是为什么不将 ItemsControl 绑定(bind)到 ObservableCollectio
最近想将我的 WPF Prism 应用程序迁移到 7.1,这样做时我遇到了有关 Unity.Abstractions 的丢失引用错误。 将 Prism nuget 软件包升级到 Prism7.1,如下
在我的根 View 模型中,我称之为 await _navigationService.NavigateAsync( "/NavigationPage/Page1of2", useMo
在 2.1.x 时间框架中讨论了与 Prism 的集成,我们是 @2.4.x,Prism 4 现已推出,/contrib 中的 RIStockTrader 示例只是一个默认的 XAML 项目。 它应该
我正在使用 Prism4,并且在我的一个模块中,我试图用一个区域注册一个 View ,并处理它的按钮单击事件(当用户单击 View 上的按钮时发布)。 public class MyModule :
几个月来,我一直在将 Prism 2.0 用于个人项目。我最近听说过 Caliburn,我想知道是否有令人信服的理由让我考虑这样做。 我喜欢 Prism 的动态模块加载能力。我打算为我的应用程序构建模
我正在尝试对我的 Windows 应用商店应用程序中的暂停事件使用react。我添加了适当的回调方法,但遇到了问题: App.Current.Suspending += Current_Suspend
我目前正在尝试完成 this tutorial让 Prism 与 Spring.net 一起工作。 通过 NuGet(或手动引用程序集)引用 Prism4 和 Spring.Net 后,设置 Boot
几个月来,我一直在使用带有以下 XAML 命名空间声明的 Prism 6: xmlns:prism="http://www.codeplex.com/prism" 但我注意到这个命名空间 URL(重定
我在 PRISM 中实现应用程序,它需要从 dll 文件中动态导入模块。我设法做到了 - 他们正在导入,但我无法显示它。我决定创建一个特殊的模块来封装它——让我们称之为 ModuleDock。所以我们
我要创建的是一个 Silverlight 应用程序,其中包含几个选项卡/模块,这些选项卡/模块都是单独的 DLL。 我看到 PRISM 具有似乎针对 UI 的 Shell/Module 概念,并且我找
请帮忙 - 我迷路了! 我正在编写一个具有一些控件和一些屏幕的小型桌面应用程序。这应该稍后与一个小型网站集成,也有一些屏幕。这个想法是让用户编辑视频并选择图像,然后与她的 friend 分享他们的结果
我在使用方法时遇到问题 this.regionManager.RegisterViewWithRegion("TextRegion", typeof(TextView)); 如果我以某种方法在 Boo
我是一名优秀的程序员,十分优秀!