c# - Memory leak in WPF app due to DelegateCommand -
I just wrote the desktop app in WPF and used the C # MVVM pattern. In this app I used representative command implementation to wrap ICommands properties highlighted in my model view. The problem is that these delegations prevent my model view and view from being garbled after the closure of the Delayed Comand View. So, until I finish the whole application, this difference remains. I give a profile to the application. I think that it is about the delegation that how can I survive this situation to maintain the model in memory and it is in the nature of the MVVM pattern, or is it about my implantation of the pattern ? Thank you.
Edit: This is the small but full part of how I implement the MVVM pattern
First of all: CommandDelegte class
Category delegate group: ICommand {personal action & lt; Object & gt; Executed; Private Prickett & lt; Object & gt; CanExcute; Public Representative Representative (Action & lt; Object & gt; Execute, Execute & Object; can) Examinations {If Execute (== Zero) {Minimum Execution of New Logic ("Execute") ; } This.execute = Executed; This.canExcute = canExecute; } Public Bull CanExecute (object parameter) {if (this.canExcute! = Null) {return canExcute (parameter); } Back true; } Public Event EventHandler CanExecuteChanged {addManager.RequerySuggested + = value; } Remove {CommandManager.RequerySuggested - = Value; }} Execute Public Zero (object parameter) {this.execute (parameter); }}
Second: ModelView class
public class ViewModel: DependencyObject, INotifyPropertyChanged {Private DelegateCommand printCommand; Public icemond print command {get {if (printCommand == null} {printCommand = new DelegateCommand (print, CanExecutePrint); } Return print; }} Zero print (object obj) {Console.WriteLine ("print command"); } Bool CanExecutePrint (object obj) {return true; } Private Event PropertyChangedEventHandler PropertyChanged; Private Zero OnProeprtyChanged (string propertyName) {If (PropertyChanged! = Null) {PropertyChanged (New, PropertyChangedEventArgs (propertyName)); }}}
Third: Back Window Code
Public MainWindow () {InitializeComponent (); Base.DataContext = New ViewModel (); }
Forward: My XML
& lt; Window x: orbit = "wpfApplication1.MainWindow" xmlns = "http://schemas.microsoft.com/enfx/ 2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/ Xaml "title =" main window "height =" 350 "width =" 525 "& gt; & Lt; Window.InputBindings & gt; & Lt; Keyboard key = "P" modifiers = "control" command = "{binding path = print command}" /> & Lt; /Window.InputBindings> & Lt; StackPanel & gt; & Lt; Button content = "print - Ctrl + P" width = "75" height = "75" command = "{binding path = print command}" /> & Lt; / StackPanel & gt;
In your case, what is the context in it << In execute
and canExecute
properties in
are examples of methods of reference. Deluxe Commands
in ViewModel
ViewModel
ViewModel
in delegate
- its PrintCommand
property.
In this view there is any number in the context of ViewModel
.
CommandManager
has a RequerySuggested
event in the context of its DelegateCommand
.
This final reference is a special case: commandmanager
its WeakReference
is used by its RequerySuggested
In the event, therefore, despite the fact that delayed command
registers for that event, it can still be garbage
Seeing all this, you will not have problems needed. If the scene is resolved, then ViewModel
and DelegateCommand
should be accessible.
You say that you have certified the application and delegation
is placed in the context of ViewModel
. I think that the logical next question should be: What is the context of representative
? It should not be commandmenager
. Do you have anything else in your application that refers to your orders?
Comments
Post a Comment