19 using System.ComponentModel;
21 using System.Windows.Input;
23 using System.Windows.Threading;
25 using System.Threading.Tasks;
31 #region Public properties 33 public virtual string DisplayString
37 return _displayString;
41 if (value != _displayString)
43 _displayString = value;
44 OnPropertyChanged(
"DisplayString");
49 public bool IsSelected
58 OnPropertyChanged(
"IsSelected");
62 public Dispatcher ViewDispatcher {
get;
set; }
66 #region Public methods 79 object wrappedObject = RevealModelObject();
80 if (model == null || wrappedObject == null)
86 return wrappedObject.Equals(model);
92 #region Public injectors 102 public Window InjectInto<T>() where T : Window,
new()
105 return InjectInto(view);
118 EventHandler h = null;
119 h = (sender, args) =>
121 this.RequestCloseView -= h;
122 view.Dispatcher.BeginInvoke(
new Action(() =>
124 view.DataContext = null;
128 this.RequestCloseView += h;
129 view.DataContext =
this;
130 ViewDispatcher = view.Dispatcher;
143 public void InjectAndShowInThread<T>(IntPtr ownerForm) where T: Window,
new()
145 Thread t =
new Thread(() =>
148 EventHandler h = null;
149 h = (sender, args) =>
151 this.RequestCloseView -= h;
153 view.Dispatcher.Invoke(
new Action(view.Close));
154 view.Dispatcher.InvokeShutdown();
156 this.RequestCloseView += h;
157 ViewDispatcher = view.Dispatcher;
158 view.DataContext =
this;
165 view.ShowInForm(ownerForm);
166 System.Windows.Threading.Dispatcher.Run();
168 t.SetApartmentState(ApartmentState.STA);
177 public void InjectAndShowInThread<T>() where T: Window,
new()
179 InjectAndShowInThread<T>(IntPtr.Zero);
189 public void InjectAndShowDialogInThread<T>(IntPtr ownerForm) where T : Window,
new()
191 Thread t =
new Thread(() =>
194 EventHandler h = null;
195 h = (sender, args) =>
197 this.RequestCloseView -= h;
199 view.Dispatcher.Invoke(
new Action(view.Close));
200 view.Dispatcher.InvokeShutdown();
202 this.RequestCloseView += h;
203 ViewDispatcher = view.Dispatcher;
204 view.DataContext =
this;
211 view.ShowDialogInForm(ownerForm);
214 t.SetApartmentState(ApartmentState.STA);
220 #region Public (abstract) methods 234 public abstract object RevealModelObject();
250 public ICommand CloseViewCommand
254 if (_closeViewCommand == null)
257 parameter => { DoCloseView(); },
258 parameter => {
return CanCloseView(); }
261 return _closeViewCommand;
277 Dispatcher = Dispatcher.CurrentDispatcher;
279 if (SynchronizationContext.Current != null)
281 SyncContext = TaskScheduler.FromCurrentSynchronizationContext();
287 #region INotifyPropertyChanged interface 289 public event PropertyChangedEventHandler PropertyChanged;
293 #region Protected methods 295 protected virtual void OnPropertyChanged(
string propertyName)
297 if (PropertyChanged != null)
299 PropertyChanged(
this,
new PropertyChangedEventArgs(propertyName));
303 protected virtual bool CanCloseView()
308 protected virtual void DoCloseView()
310 if (RequestCloseView != null && CanCloseView())
312 RequestCloseView(
this, EventArgs.Empty);
321 protected CancellationToken
Dispatch(Action action)
323 if (SyncContext == null)
325 Logger.Info(
"Dispatch: Dispatching with dispatcher");
326 Dispatcher.Invoke(action);
327 return CancellationToken.None;
331 Logger.Info(
"Dispatch: Dispatching on current synchronization context");
333 CancellationToken token =
new CancellationToken();
334 Task.Factory.StartNew(action, token, TaskCreationOptions.None, SyncContext);
341 #region Protected properties 347 protected Dispatcher Dispatcher {
get;
private set; }
349 protected TaskScheduler SyncContext {
get;
private set; }
353 #region Private fields 355 private string _displayString;
357 private bool _isSelected;
363 private static NLog.Logger Logger {
get {
return _logger.Value; } }
365 private static readonly Lazy<
NLog.Logger> _logger =
new Lazy<
NLog.Logger>(() =>
NLog.LogManager.GetCurrentClassLogger());
CancellationToken Dispatch(Action action)
Dispatches an action in the current synchronization context if one exists, or using the Dispatcher...
Command that implements ICommand and accepts delegates that contain the command implementation.
ViewModelBase()
Does not allow public instantiation of this class.
Window InjectInto(Window view)
Injects the view model into an existing view by setting the view's DataContext.
EventHandler RequestCloseView
Raised by the CloseView Command, signals that associated views are to be closed.
bool IsViewModelOf(object model)
Determines whether the current object is a view model of a particular model object.