使用Catel命令在CanExecuteChanged上发生内存泄漏

时间:2017-01-25 08:07:51

标签: c# wpf mvvm catel dotmemory

我正在分析使用Catel作为MVVM框架的WPF应用程序,并且我已经看到在ViewModel中我有2个保留路径 enter image description here

现在我在附加到上下文菜单的行为中创建了这样的RadMenuItem,它们被定义为

protected virtual IEnumerable<RadMenuItem> GetRowMenuItems(RadContextMenu contextMenu)
    {
        var rowItems = new List<RadMenuItem>();

        RadMenuItem saveSettings = new RadMenuItem
        {
            Tag = "force",
            Header = CoreResources.LBL_SAVE_SETTINGS,
            Command = DefaultRtViewContextMenuCommands.SaveLayoutDataCommand,
            CommandParameter = AssociatedObject,

            Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/IF.Tesoreria.Client.WPF.Core;component/Media/save.png")) }
        };

        rowItems.Add(saveSettings);

        RadMenuItem loadSettings = new RadMenuItem
        {
            Tag = "force",
            Header = CoreResources.LBL_LOAD_SETTINGS,
            Command = DefaultRtViewContextMenuCommands.LoadLayoutDataCommand,
            CommandParameter = AssociatedObject,
            Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/IF.Tesoreria.Client.WPF.Core;component/Media/load.png")) }
        };

现在命令在

中定义
    public class DefaultRtViewContextMenuCommands
{
   public static Command<FlexGridCommandParameter> SaveLayoutDataCommand { get; private set; }
    public static Command<FlexGridCommandParameter> LoadLayoutDataCommand { get; private set; }

    private static void OnLoadLayoutCommandExecute(FlexGridCommandParameter grid)
    {
        Argument.IsNotNull(() => grid);

        var dependencyResolver = DependencyResolverManager.Default;
        var openFileService = dependencyResolver.DefaultDependencyResolver.Resolve<IOpenFileService>();
        openFileService.Filter = "Gridview setting file|*.flexgrid";
        if (openFileService.DetermineFile())
        {
            // User selected a file
            using (var fs = File.OpenRead(openFileService.FileName))
            {
                GridViewPersistenceHelper.LoadLayout(grid.Grid, fs, null);
            }
        }
    }

    private static void OnSaveLayoutCommandExecute(FlexGridCommandParameter grid)
    {
        Argument.IsNotNull(() => grid);

        var dependencyResolver = DependencyResolverManager.Default;
        var saveFileService = dependencyResolver.DefaultDependencyResolver.Resolve<ISaveFileService>();
        saveFileService.Filter = "Gridview setting file|*.flexgrid";
        if (saveFileService.DetermineFile())
        {
            // User selected a file
            using (var fs = File.OpenWrite(saveFileService.FileName))
            {
                GridViewPersistenceHelper.SaveLayout(grid.Grid, fs);
            }
        }
    }

 static DefaultRtViewContextMenuCommands()
    {
        viewModelFactory = ServiceLocator.Default.ResolveType<IViewModelFactory>();
        portfolioService = ServiceLocator.Default.ResolveType<IPortfoliosService>();
        pkInstrumentsService = ServiceLocator.Default.ResolveType<IPkInstrumentsService>();

        SaveLayoutDataCommand = new Command<FlexGridCommandParameter>(OnSaveLayoutCommandExecute,_=>true);
        LoadLayoutDataCommand = new Command<FlexGridCommandParameter>(OnLoadLayoutCommandExecute,_=>true);
    }

我做错了什么? 感谢

1 个答案:

答案 0 :(得分:1)

radMenuItem.Command = null;

适合我。您可以反编译并在执行此操作时看到该菜单项从Command的CanExecuteChanged

取消注册