bovender framework
C# framework that implements MVVM and more
ProcessMessageContent.cs
1 /* ProcessMessageContent.cs
2  * part of Daniel's XL Toolbox NG
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
19 using System;
20 using System.ComponentModel;
21 
22 namespace Bovender.Mvvm.Messaging
23 {
31  {
32  #region Public properties
33 
34  public bool Processing
35  {
36  get { return _processing; }
37  set
38  {
39  _processing = value;
40  OnPropertyChanged("Processing");
41  }
42  }
43 
44  public bool IsIndeterminate
45  {
46  get { return _isIndeterminate; }
47  set
48  {
49  _isIndeterminate = value;
50  OnPropertyChanged("IsIndeterminate");
51  }
52  }
53 
54  public int PercentCompleted
55  {
56  get
57  {
58  return _percentCompleted;
59  }
60  set
61  {
62  _percentCompleted = value;
63  OnPropertyChanged("PercentCompleted");
64  }
65  }
66 
67  public bool WasSuccessful
68  {
69  get { return _wasSuccessful; }
70  set
71  {
72  _wasSuccessful = value;
73  OnPropertyChanged("WasSuccessful");
74  }
75  }
76 
77  public bool WasCancelled
78  {
79  get { return _wasCancelled; }
80  set
81  {
82  _wasCancelled = value;
83  OnPropertyChanged("WasCancelled");
84  }
85  }
86 
91  public Exception Exception
92  {
93  get { return _exception; }
94  set
95  {
96  _exception = value;
97  OnPropertyChanged("Exception");
98  }
99  }
100 
104  public Action CancelProcess
105  {
106  get
107  {
108  return _cancelProcess;
109  }
110  set
111  {
112  _cancelProcess = value;
113  OnPropertyChanged("CancelProcess");
114  }
115  }
116 
117  #endregion
118 
119  #region Commands
120 
121  public DelegatingCommand CancelCommand
122  {
123  get
124  {
125  if (_cancelCommand == null)
126  {
127  _cancelCommand = new DelegatingCommand(
128  (param) => DoCancel(),
129  (param) => CanCancel()
130  );
131  }
132  return _cancelCommand;
133  }
134  }
135 
136  #endregion
137 
138  #region MVVM messages
139 
140  public Message<ProcessMessageContent> CompletedMessage
141  {
142  get
143  {
144  if (_completedMessage == null)
145  {
146  _completedMessage = new Message<ProcessMessageContent>();
147  }
148  return _completedMessage;
149  }
150  }
151 
152  #endregion
153 
154  #region Constructors
155 
159  public ProcessMessageContent() : base() { }
160 
168  public ProcessMessageContent(ViewModelBase viewModel) : base(viewModel) { }
169 
176  public ProcessMessageContent(Action cancelProcess)
177  : this()
178  {
179  CancelProcess = cancelProcess;
180  }
181 
191  public ProcessMessageContent(ViewModelBase viewModel, Action cancelProcess)
192  : base(viewModel)
193  {
194  CancelProcess = cancelProcess;
195  }
196 
197  #endregion
198 
199  #region Protected methods
200 
206  protected void DoCancel()
207  {
208  if (CanCancel())
209  {
210  Processing = false;
211  WasSuccessful = false;
212  WasCancelled = true;
213  CancelProcess.Invoke();
214  CompletedMessage.Send(this, null);
215  DoCloseView();
216  }
217  }
218 
219  protected bool CanCancel()
220  {
221  return (CancelProcess != null);
222  }
223 
224  #endregion
225 
226  #region Private fields
227 
228  private int _percentCompleted;
229  private DelegatingCommand _cancelCommand;
230  private Message<ProcessMessageContent> _completedMessage;
231  private bool _processing;
232  private bool _wasCancelled;
233  private bool _wasSuccessful;
234  private Exception _exception;
235  private Action _cancelProcess;
236  private bool _isIndeterminate;
237 
238  #endregion
239  }
240 }
Holds information about percent completion of a process and defines events that occur when the proces...
ProcessMessageContent()
Creates a new ProcessMessageContent.
Message content that holds a reference to a view model.
Command that implements ICommand and accepts delegates that contain the command implementation.
void DoCancel()
Cancels the process, sends a message to any subscribed views informing about the state change...
ProcessMessageContent(Action cancelProcess)
Creates a new ProcessMessageContent that has the ability to cancel a process.
ProcessMessageContent(ViewModelBase viewModel)
Creates a new ProcessMessageContent that encapsulates the given viewModel to enable views to access ...
ProcessMessageContent(ViewModelBase viewModel, Action cancelProcess)
Creates a new ProcessMessageContent that has the ability to cancel a process and that encapsulates a ...