内存泄漏在WPF应用程序由于DelegateCommand [英] Memory leak in WPF app due to DelegateCommand

查看:126
本文介绍了内存泄漏在WPF应用程序由于DelegateCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成使用MVVM模式编写的WPF桌面应用程序和C#。在这个软件,我用委派代表来指挥实现包装在我的模型视图露出个ICommand属性。问题是这些DelegateCommands防止我的模型视图并查看关闭视图后被垃圾回收。所以它保持乱蹦乱跳,直到我终止整个应用程序。我个人资料,我觉得它是所有关于delegatecommand,保持模型视图在内存中的应用程序。
我怎么能避免出现这种情况,是这MVVM模式的性质,或者它是关于我的模式植入?谢谢

I just finished desktop apps written in WPF and c# using MVVM pattern. In this app I used Delegate Command implementation to wrap the ICommands properties exposed in my ModelView. The problem is these DelegateCommands prevent my ModelView and View from being garbage collected after closing the view. So it stays larking until I terminate the whole application. I profile the application I find it’s all about delegatecommand that keeping the modelview in memory. How could I avoid this situation and is this in nature of mvvm pattern, or it’s about my implantation of the pattern?. Thanks.

编辑:这是我如何实现MVVM模式小而全部分

this is small but complete portion of how i implement MVVM pattern

第一:CommandDelegte类

First: CommandDelegte class

class DelegateCommand:ICommand
{
    private Action<object> execute;
    private Predicate<object> canExcute;
    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }
        this.execute = execute;
        this.canExcute = canExecute;
    }
    public bool CanExecute(object parameter)
    {
        if (this.canExcute != null)
        {
            return canExcute(parameter);
        }
        return true;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }


    public void Execute(object parameter)
    {
        this.execute(parameter);
    }
}



二:模型视图类

Second: ModelView Class

public class ViewModel:DependencyObject, INotifyPropertyChanged
{
    private DelegateCommand printCommand;

    public ICommand PrintCommand
    {
        get
        {
            if (printCommand == null)
            {
                printCommand = new DelegateCommand(Print, CanExecutePrint);
            }
            return printCommand;
        }
    }
    void Print(object obj)
    {
        Console.WriteLine("Print Command");

    }
    bool CanExecutePrint(object obj)
    {
        return true;
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void OnProeprtyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}



第三:后面<窗口代码/ p>

Third: Window code behind

public MainWindow()
    {
        InitializeComponent();
        base.DataContext = new ViewModel();
    }



第四:我的XAML

Forth: My XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.InputBindings>
    <KeyBinding Key="P" Modifiers="Control" Command="{Binding Path=PrintCommand}"/>
</Window.InputBindings>
<StackPanel>
    <Button Content="Print - Ctrl+P" Width="75" Height="75" Command="{Binding Path=PrintCommand}"/>
</StackPanel>



推荐答案

在你的情况,有什么包含什么?

In your case, what contains a reference to what?


  1. DelegateCommand 包含视图模型的参考 - 它的执行 canExecute 属性包含的视图模型实例的方法的引用。

  1. DelegateCommand contains a reference to ViewModel - its execute and canExecute properties contain references to a methods of the ViewModel instance.

视图模型包含一个参考 DelegateCommand - 它的 PrintCommand 属性

ViewModel contains a reference to DelegateCommand - its PrintCommand property.

该视图包含任意数量的视图模型

The view contains any number of references to the ViewModel.

命令管理包含在其 RequerySuggested DelegateCommand C $ C>事件

The CommandManager contains a reference to DelegateCommand in its RequerySuggested event.

这是最后一个引用是一种特殊情况:命令管理使用的WeakReference RequerySuggested 事件,所以尽管该事件DelegateCommand 寄存器,它仍然是垃圾收集。

That last reference is a special case: CommandManager uses a WeakReference in its RequerySuggested event, so despite the fact that DelegateCommand registers for that event, it can still be garbage-collected.

考虑到这一切,你不应该有问题。如果视图得到处理,无论是视图模型还是 DelegateCommand 应该是可达的。

Given all this, you shouldn't be having a problem. If the view gets disposed, neither the ViewModel nor the DelegateCommand should be reachable.

您说您已经成型的应用和 DelegateCommand 持有至视图模型参考。在我看来,逻辑下一个问题应该是:什么是抱着 DelegateCommand 的参考?它不应该是命令管理。你有你的应用程序的引用你的命令别的东西吗?

You say you've profiled the application and DelegateCommand is holding a reference to ViewModel. It seems to me that the logical next question should be: what's holding a reference to DelegateCommand? It shouldn't be CommandManager. Do you have something else in your application that's referencing your commands?

这篇关于内存泄漏在WPF应用程序由于DelegateCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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