1

Topic: How to make check at closing of a window within the limits of MVVM?

I welcome. There is a window. At its closing it is necessary to ask the user, whether he wants it and not to close a window if does not want. How it to implement in categories MVVM? Whether I.e., likely, it is necessary to get whence a sign we want to close a window at window closing. Prompt, please, where it to find? I implemented it so, after performance of a command irrespective of result of dialogue the window is closed. Correct me please: <Window... SizeToContent = "WidthAndHeight" WindowStartupLocation = "CenterScreen" xmlns:i = "http://schemas.microsoft.com/expression … eractivity"> <i:Interaction. Triggers> <i:EventTrigger EventName = "Closing"> <i:InvokeCommandAction Command = "{Binding Path=WWindowClosingCommand}"/> </i:EventTrigger> </i:Interaction. Triggers>... </Window> public class ViewModelClass: Notifier, IViewModel {#region variables///<summary> object of model private </summary> private IModel model; ///<summary> object of model public </summary> public IModel Model {get {return model;}}///<summary> a command of closing of the form private </summary> private ICommand wWindowClosingCommand;///<summary> a command of closing of the form public </summary> public ICommand WWindowClosingCommand {get {return wWindowClosingCommand;} set {wWindowClosingCommand = value; NotifyPropertyChanged ("WWindowClosingCommand");}} #endregion #region functions///<summary> the designer of class ViewModelClass </summary>///<param name = "_ model"> object of model </param> public ViewModelClass (IModel _model) {model = _model; WWindowClosingCommand = new WindowClosingCommandClass (this);} ... #endregion}///<summary> a command of closing of the form </summary> public class WindowClosingCommandClass: IWindowClosingCommand {#region structures and classes #endregion #region variables///<summary> object model-representation private </summary> private IViewModel viewModel;///<summary> object model-representation public </summary> public IViewModel ViewModel {get {return viewModel;}}///<summary> the event happening at origin of changes, influencing, whether the given command </summary> public event EventHandler CanExecuteChanged should be fulfilled; #endregion #region functions///<summary> the designer of class WindowClosingCommandClass </summary>///<param name = "_ view_model"> object of model-representation </param> public WindowClosingCommandClass (IViewModel _view_model) {viewModel = _view_model; CanExecuteChanged = delegate {};} ///<summary> check that the command can be fulfilled </summary> public bool CanExecute (object parameter) {Boolean result = false;//if (closeProp == 0)//{if (System. Windows. Forms. MessageBox. Show ("you are assured, what want to close a window?", "warning", System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Warning) == System. Windows. Forms. DialogResult. OK) {result = true;} else {result = false;}//} return result;}///<summary> to execute a command </summary> public void Execute (object parameter) {...} #endregion}

2

Re: How to make check at closing of a window within the limits of MVVM?

Hello, lesovick, you wrote: L> I Welcome. L> there is a window. At its closing it is necessary to ask the user, whether he wants it and not to close a window if does not want. How it to implement in categories MVVM? Whether I.e., likely, it is necessary to get whence a sign we want to close a window at window closing. Prompt, please, where it to find? I implemented it so, after performance of a command irrespective of result of dialogue the window is closed. Correct me please: L> L> <Window... L> SizeToContent = "WidthAndHeight" L> WindowStartupLocation = "CenterScreen" L> xmlns:i = "http://schemas.microsoft.com/expression … eractivity"> L> <i:Interaction. Triggers> L> <i:EventTrigger EventName = "Closing"> L> <i:InvokeCommandAction Command = "{Binding Path=WWindowClosingCommand}"/> L> </i:EventTrigger> L> </i:Interaction. Triggers> L>... L> </Window> L> L> L> public class ViewModelClass: Notifier, IViewModel L> {L> #region variables L>///<summary> object of model private </summary> L> private IModel model; L>///<summary> object of model public </summary> L> public IModel Model {get {return model;}} L>///<summary> a command of closing of the form private </summary> L> private ICommand wWindowClosingCommand; L>///<summary> a command of closing of the form public </summary> L> public ICommand WWindowClosingCommand {get {return wWindowClosingCommand;} set {wWindowClosingCommand = value; NotifyPropertyChanged ("WWindowClosingCommand");} } L> #endregion L> #region functions L>///<summary> the designer of class ViewModelClass </summary> L>///<param name = "_ model"> object of model </param> L> public ViewModelClass (IModel _model) L> {L> model = _model; L> WWindowClosingCommand = new WindowClosingCommandClass (this); L>} L>... L> #endregion L>} L> L> L>///<summary> a command of closing of the form </summary> L> public class WindowClosingCommandClass: IWindowClosingCommand L> {L> #region structures and classes L> #endregion L> #region variables L>///<summary> object model-representation private </summary> L> private IViewModel viewModel; L>///<summary> object model-representation public </summary> L> public IViewModel ViewModel {get {return viewModel;} } L>///<summary> the event happening at origin of changes, influencing, whether the given command </summary> L> public event EventHandler CanExecuteChanged should be fulfilled; L> #endregion L> #region functions L>///<summary> the designer of class WindowClosingCommandClass </summary> L>///<param name = "_ view_model"> object of model-representation </param> L> public WindowClosingCommandClass (IViewModel _view_model, ISynchronizator _synchronizator) L> {L> viewModel = _view_model; L> CanExecuteChanged = delegate {}; L>} L>///<summary> check that the command can be fulfilled </summary> L> public bool CanExecute (object parameter) L> {L> Boolean result = false; L>//if (closeProp == 0) L>//{L> if (System. Windows. Forms. MessageBox. Show ("you are assured, what want to close a window?" "Warning", System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Warning) == System. Windows. Forms. DialogResult. OK) L> {L> result = true; L>} L> else {result = false;} L>//} L> return result; L>} L>///<summary> to execute a command </summary> L> public void Execute (object parameter) L> {L>... L>} L> #endregion L>} L> it is necessary to Do here:///<summary> to execute a command </summary> L> public void Execute (object parameter) L> {L>... L>} L> #endregion And in a command it is necessary to transfer parameter of closing of window CancelEventArgs and in it to install a flag.

3

Re: How to make check at closing of a window within the limits of MVVM?

Hello, Qulac, you wrote: Q> And in a command it is necessary to transfer parameter of closing of window CancelEventArgs and in it to install a flag. Fine. Only how to transfer parameter CancelEventArgs in a command, prompt please. Where it to register?

4

Re: How to make check at closing of a window within the limits of MVVM?

Hello, lesovick, you wrote: L> Hello, Qulac, you wrote: Q>> And in a command it is necessary to transfer parameter of closing of window CancelEventArgs and in it to install a flag. L> it is healthy. Only how to transfer parameter CancelEventArgs in a command, prompt please. Where it to register? Here: the link like all methods are presented.

5

Re: How to make check at closing of a window within the limits of MVVM?

Hello, Qulac, you wrote: Q> Here: the link like all methods are presented. Hurrah! It's a go. Many sendings saw, where similar suggested to solve with the help behavior. But here, me it seemed, operation is more in detail and most clearly described with them. Big to you thanks! The following code if it will be interesting to someone as a result turns out: <Window...> <i:Interaction. Behaviors> <local:EventToCommandBehavior Command = "{Binding ClosingCommand}" Event = "Closing" PassArguments = "True"/> </i:Interaction. Behaviors>... </Window> public class ViewModelClass5 {private ModelClass5 model; public ModelClass5 Model {get {return model;} set {model = value;}} public ActionCommand <CancelEventArgs> ClosingCommand {get; private set;} public ViewModelClass5 (ModelClass5 _model) {_model = _model; this. ClosingCommand = new ActionCommand <CancelEventArgs> (OnClosing);} private void OnClosing (CancelEventArgs e) {e. Cancel = true;} }///<summary>///Behavior that will connect an UI event to a viewmodel Command,///allowing the event arguments to be passed as the CommandParameter.///</summary> public class EventToCommandBehavior: Behavior <FrameworkElement> {private Delegate _handler; private EventInfo _oldEvent;//Event public string Event {get {return (string) GetValue (EventProperty);} set {SetValue (EventProperty, value);}} public static readonly DependencyProperty EventProperty = DependencyProperty. Register ("Event", typeof (string), typeof (EventToCommandBehavior), new PropertyMetadata (null, OnEventChanged));//Command public ICommand Command {get {return (ICommand) GetValue (CommandProperty);} set {SetValue (CommandProperty, value);} } public static readonly DependencyProperty CommandProperty = DependencyProperty. Register ("Command", typeof (ICommand), typeof (EventToCommandBehavior), new PropertyMetadata (null));//PassArguments (default: false) public bool PassArguments {get {return (bool) GetValue (PassArgumentsProperty);} set {SetValue (PassArgumentsProperty, value);}} public static readonly DependencyProperty PassArgumentsProperty = DependencyProperty. Register ("PassArguments", typeof (bool), typeof (EventToCommandBehavior), new PropertyMetadata (false)); private static void OnEventChanged (DependencyObject d, DependencyPropertyChangedEventArgs e) {var beh = (EventToCommandBehavior) d; if (beh. AssociatedObject! = null)//is not yet attached at initial load beh. AttachHandler ((string) e. NewValue);} protected override void OnAttached () {AttachHandler (this. Event);//initial set}///<summary>///Attaches the handler to the event///</summary> private void AttachHandler (string eventName) {//detach old event if (_oldEvent! = null) _oldEvent. RemoveEventHandler (this. AssociatedObject, _handler);//attach new event if (! string. IsNullOrEmpty (eventName)) {EventInfo ei = this. AssociatedObject. GetType ().GetEvent (eventName); if (ei! = null) {MethodInfo mi = this. GetType ().GetMethod ("ExecuteCommand", BindingFlags. Instance | BindingFlags. NonPublic); _handler = Delegate. CreateDelegate (ei. EventHandlerType, this, mi); ei. AddEventHandler (this. AssociatedObject, _handler); _oldEvent = ei;//store to detach in case the Event property changes} else throw new ArgumentException (string. Format ("The event ' {0} ' was not found on type ' {1} '", eventName, this. AssociatedObject. GetType ().Name));}}///<summary>///Executes the Command///</summary> private void ExecuteCommand (object sender, EventArgs e) {object parameter = this. PassArguments? e: null; if (this. Command! = null) {if (this. Command. CanExecute (parameter)) this. Command. Execute (parameter);}}} public class ActionCommand <T>: ICommand {public event EventHandler CanExecuteChanged; private Action <T> _action; public ActionCommand (Action <T> action) {_action = action;} public bool CanExecute (object parameter) {return true;} public void Execute (object parameter) {if (_action! = null) {var castParameter = (T) Convert. ChangeType (parameter, typeof (T)); _action (castParameter);}}}

6

Re: How to make check at closing of a window within the limits of MVVM?

Hello, lesovick, you wrote: At all I do not know, you it is serious to "it" are glad? Why in essence it was impossible (dirty MVVM) to subscribe on event in View explicitly, to delegate it in ViewModel (using the interface if view  different models) causing a method, to return false and easy to install e. Cancel in true? That for dancings with a tambourine including: 1) Interraction. Behavior 2) the Behavior 3) yes a little reflection 4) and on , we transfer CancelEventArgs type in ViewModel. Pleasure from what? I can is finite I carp, after all itself transferred SecurePassword from View in ViewModel, but at me an excuse - on another in any way.

7

Re: How to make check at closing of a window within the limits of MVVM?

Hello, Sinatr, you wrote: S> Hello, lesovick, you wrote: S> At all I do not know, you it is serious to "it" are glad? S> why in essence it was impossible (dirty MVVM) to subscribe for event in View explicitly, to delegate it in ViewModel (using the interface if view  different models) causing a method, to return false and easy to install e. Cancel in true? S> That for dancings with a tambourine including: 1) Interraction. Behavior 2) the Behavior 3) yes a little reflection 4) and on , we transfer CancelEventArgs type in ViewModel. Pleasure from what? I can is finite I carp, after all itself transferred SecurePassword from View in ViewModel, but at me an excuse - on another in any way.  the crime  CancelEventArgs in VM subtilized certainly, but in what?

8

Re: How to make check at closing of a window within the limits of MVVM?

Hello, Sinatr, you wrote: S> Hello, lesovick, you wrote: S> At all I do not know, you it is serious to "it" are glad? S> why in essence it was impossible (dirty MVVM) to subscribe for event in View explicitly, to delegate it in ViewModel (using the interface if view  different models) causing a method, to return false and easy to install e. Cancel in true? S> That for dancings with a tambourine including: 1) Interraction. Behavior 2) the Behavior 3) yes a little reflection 4) and on , we transfer CancelEventArgs type in ViewModel. Pleasure from what? I can is finite I carp, after all itself transferred SecurePassword from View in ViewModel, but at me an excuse - on another in any way. Well, if it would be desirable MVVM-cleanliness it is necessary to do somehow so: viewmodel is able to do two things 1) request about closing of a window 2) response to event of closing of a window (through a dagger the user, through shutdown systems - not an essence) And all this UI-interaktivchik "precisely want to close? Did not change the mind? The last time I ask "quite to myself fulfills behavior