如何处理mvvm wpf模型中的中继命令 [英] How to dispose off the relay command in mvvm wpf model

查看:187
本文介绍了如何处理mvvm wpf模型中的中继命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序使用MVVM wpf模型以及约翰·史密斯的中继命令类。它有两个主要的问题:

I have this app using MVVM wpf model along with John Smith's relay command class. There are two main issues with it:

1)即使处理了视图模型并将命令设置为null,它们仍然被激发。

1) Even after disposing the view model and setting the commands to null, they are still fired afterwards.

2)视图模型虽然被处理掉,但仍然在内存中。它使用顶部的选项卡控件,即使关闭选项卡后内存也不会被清除。这与视图模型相关,因为一旦视图模型的属性设置为null,下一次打开不同的选项卡时,视图模型将尝试访问已废弃的属性。 FYI这里没有涉及单例。

2) The view model although disposed off, still seems to be in memory. It's using tab control on top and the memory never gets cleaned even after closing the tabs. This is related to the view model since once the properties of the view model are set to null, the next time you open a different tab the view model tries to access the property that was disposed off. FYI there are no singletons involved here.

它使用继电器命令类,并且问题最终在于这里的命令被触发,即使目标对象没有提高它是一个命令链接到的按钮没有被点击,但仍然会在关闭其子窗口时触发。

It is using the relay command class and the problem eventually lies here where the commands are being fired even though the target object has not raised it i.e. the button a command is linked to is not clicked but still it fires it when closing its child window.

任何帮助将非常感激。

感谢

推荐答案

将命令属性的PropertyChanged事件设置为null

Raise the PropertyChanged event for the command properties after you set them to null.

public class ViewModel : INotifyPropertyChanged, IDisposable
{
   public event PropertyChangedEventHandler PropertyChanged;
   public void Dispose()
   {
      Command = null;
   }

   public RelayCommand Command
   {
      get{return m_command;}
      set
      {
         if(m_command == value)
            return;
         m_command = value;
         if (PropertyChanged != null)
            PropertyChanged (this, new PropertyChangedEventArgs ("Command");
      }
   }
}

这篇关于如何处理mvvm wpf模型中的中继命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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