bovender framework
C# framework that implements MVVM and more
MessageActionBase.cs
1 /* MessageActionBase.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  */
18 using System;
19 using System.Windows;
20 using System.Windows.Interactivity;
21 using Bovender.Mvvm.Messaging;
23 using Bovender.Extensions;
24 
25 namespace Bovender.Mvvm.Actions
26 {
32  public abstract class MessageActionBase : TriggerAction<FrameworkElement>
33  {
34  #region Public properties
35 
36  public MessageContent Content { get; set; }
37 
38  #endregion
39 
40  #region TriggerAction overrides
41 
47  protected override void Invoke(object parameter)
48  {
49  dynamic args = parameter as MessageArgsBase;
50  dynamic content = null;
51  dynamic response = null;
52  if (args != null)
53  {
54  content = args.Content;
55  response = args.Respond;
56  }
57  else
58  {
59  Logger.Warn("Invoke: Parameter is not a MessageArgsBase");
60  }
61  Invoke(content, response);
62  }
63 
64  protected void Invoke<T>(T messageContent, Action response)
65  where T : MessageContent
66  {
67  Content = messageContent;
68  ViewModelBase viewModel = GetDataContext(messageContent);
69  Window view = CreateView();
70  if (view != null)
71  {
72  if (viewModel != null)
73  {
74  EventHandler responseHandler = null;
75  responseHandler = (sender, e) =>
76  {
77  viewModel.RequestCloseView -= responseHandler;
78  if (response != null) response();
79  };
80  viewModel.RequestCloseView += responseHandler;
81  if (view.DataContext == null)
82  {
83  viewModel.InjectInto(view);
84  }
85  else
86  {
87  Logger.Fatal("Invoke: Got view from CreateView() that already has a data context!");
88  Logger.Fatal("Invoke: Data context must be returned from GetDataContext() only!");
89  throw new InvalidOperationException("CreateView() must not create a view with a data context");
90  }
91  }
92  else
93  {
94  Logger.Info("Invoke: Did not get view model, cannot data bind view or set up event handlers");
95  }
96  if (view.DataContext != null)
97  {
98  Logger.Info("Invoke: View is data-bound to {0}", view.DataContext.GetType().FullName);
99  }
100  else
101  {
102  Logger.Info("Invoke: View has no data context");
103  }
104  ShowView(view);
105  }
106  else
107  {
108  Logger.Warn("Invoke: CreateView did not return a Window object!");
109  }
110  }
111 
112  #endregion
113 
114  #region Virtual methods
115 
123  protected virtual void ShowView(Window view)
124  {
125  Logger.Info("ShowView: Showing view as dialog");
126  view.ShowDialogInForm();
127  }
128 
137  protected virtual ViewModelBase GetDataContext(MessageContent messageContent)
138  {
139  return messageContent;
140  }
141 
142  #endregion
143 
144  #region Abstract methods
145 
150  protected abstract Window CreateView();
151 
152  #endregion
153 
154  #region Class logger
155 
156  private static NLog.Logger Logger { get { return _logger.Value; } }
157 
158  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
159 
160  #endregion
161  }
162 }
Abstract base class for MVVM messaging actions.
Window InjectInto(Window view)
Injects the view model into an existing view by setting the view&#39;s DataContext.
EventHandler RequestCloseView
Raised by the CloseView Command, signals that associated views are to be closed.
virtual void ShowView(Window view)
Shows the view as a modal dialog.
Simple object that encapsulates a boolean value; to be used in MVVM interaction with MessageArgs...
virtual ViewModelBase GetDataContext(MessageContent messageContent)
Gets a ViewModelBase object that will be injected into the view In the abstract base class...
override void Invoke(object parameter)
Invokes the action by creating a view and a corresponding view model.