bovender framework
C# framework that implements MVVM and more
NotificationAction.cs
1 /* NotificationAction.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 Bovender.Mvvm.Views;
21 using Bovender.Mvvm.Messaging;
22 
23 namespace Bovender.Mvvm.Actions
24 {
34  {
35  #region Public (dependency) properties
36 
37  public string Caption
38  {
39  get { return (string)GetValue(CaptionProperty); }
40  set
41  {
42  SetValue(CaptionProperty, value);
43  }
44  }
45 
46  public string Message
47  {
48  get { return (string)GetValue(MessageProperty); }
49  set
50  {
51  SetValue(MessageProperty, value);
52  }
53  }
54 
55  public string Param1
56  {
57  get { return (string)GetValue(Param1Property); }
58  set
59  {
60  SetValue(Param1Property, value);
61  }
62  }
63 
64  public string Param2
65  {
66  get { return (string)GetValue(Param2Property); }
67  set
68  {
69  SetValue(Param2Property, value);
70  }
71  }
72 
73  public string Param3
74  {
75  get { return (string)GetValue(Param3Property); }
76  set
77  {
78  SetValue(Param3Property, value);
79  }
80  }
81 
82  public string OkButtonText
83  {
84  get { return (string)GetValue(OkButtonTextProperty); }
85  set
86  {
87  SetValue(OkButtonTextProperty, value);
88  }
89  }
90 
91  public string CancelButtonText
92  {
93  get { return (string)GetValue(CancelButtonTextProperty); }
94  set
95  {
96  SetValue(CancelButtonTextProperty, value);
97  }
98  }
99 
104  public string FormattedText
105  {
106  get
107  {
108  try
109  {
110  if (_formattedText == null)
111  {
112  _formattedText = String.Format(Message, Param1, Param2, Param3);
113  }
114  Logger.Info(_formattedText);
115  return _formattedText;
116  }
117  catch
118  {
119  Logger.Warn("Cannot format notification message: no Message");
120  return "*** No message text given! ***";
121  }
122  }
123  }
124 
125  #endregion
126 
127  #region Declarations of dependency properties
128 
129  public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
130  "Caption", typeof(string), typeof(NotificationAction));
131 
132  public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
133  "Message", typeof(string), typeof(NotificationAction));
134 
135  public static readonly DependencyProperty Param1Property = DependencyProperty.Register(
136  "Param1", typeof(string), typeof(NotificationAction));
137 
138  public static readonly DependencyProperty Param2Property = DependencyProperty.Register(
139  "Param2", typeof(string), typeof(NotificationAction));
140 
141  public static readonly DependencyProperty Param3Property = DependencyProperty.Register(
142  "Param3", typeof(string), typeof(NotificationAction));
143 
144  public static readonly DependencyProperty OkButtonTextProperty = DependencyProperty.Register(
145  "OkButtonText", typeof(string), typeof(NotificationAction));
146 
147  public static readonly DependencyProperty CancelButtonTextProperty = DependencyProperty.Register(
148  "CancelButtonText", typeof(string), typeof(NotificationAction));
149 
150  #endregion
151 
152  #region Constructors
153 
154  public NotificationAction()
155  : base()
156  {
157  OkButtonText = "OK";
158  }
159 
160  public NotificationAction(string caption, string message)
161  : this()
162  {
163  Caption = caption;
164  Message = message;
165  }
166 
167  public NotificationAction(string caption, string message, string okButtonText)
168  : this(caption, message)
169  {
170  OkButtonText = okButtonText;
171  }
172 
173  public NotificationAction(string caption, string message, string okButtonText, string param)
174  : this(caption, message, okButtonText)
175  {
176  Param1 = param;
177  }
178 
179  public NotificationAction(string caption, string message, string okButtonText, string param1, string param2)
180  : this(caption, message, okButtonText, param1)
181  {
182  Param2 = param2;
183  }
184 
185  public NotificationAction(string caption, string message, string okButtonText, string param1, string param2, string param3)
186  : this(caption, message, okButtonText, param1, param2)
187  {
188  Param3 = param3;
189  }
190 
191  #endregion
192 
193  #region Implementation of abstract base methods
194 
195  protected override Window CreateView()
196  {
197  return new NotificationView();
198  }
199 
200  #endregion
201 
202  #region Overrides
203 
204  protected override ViewModels.ViewModelBase GetDataContext(MessageContent messageContent)
205  {
206  if (!String.IsNullOrEmpty(Caption))
207  {
208  Logger.Info("GetDataContext: Overriding caption");
209  messageContent.Caption = Caption;
210  }
211  if (!String.IsNullOrEmpty(Message))
212  {
213  Logger.Info("GetDataContext: Overriding message");
214  messageContent.Message = FormattedText;
215  }
216  if (!String.IsNullOrEmpty(OkButtonText))
217  {
218  Logger.Info("GetDataContext: Overriding OK button text");
219  messageContent.OkButtonText = OkButtonText;
220  }
221  if (!String.IsNullOrEmpty(CancelButtonText))
222  {
223  Logger.Info("GetDataContext: Overriding OK button text");
224  messageContent.CancelButtonText = CancelButtonText;
225  }
226  return messageContent;
227  }
228 
229  #endregion
230 
231  #region Private fields
232 
233  private string _formattedText;
234 
235  #endregion
236 
237  #region Class logger
238 
239  private static NLog.Logger Logger { get { return _logger.Value; } }
240 
241  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
242 
243  #endregion
244  }
245 }
override ViewModels.ViewModelBase GetDataContext(MessageContent messageContent)
Gets a ViewModelBase object that will be injected into the view In the abstract base class...
Conveys a message from a view model to a consumer (typically, a view) that has subscribed to the Sent...
Definition: Message.cs:31
Abstract base class for MVVM messaging actions.
Interaction logic for NotificationView.xaml
override Window CreateView()
Returns a view that can bind to expected message contents.
Simple object that encapsulates a boolean value; to be used in MVVM interaction with MessageArgs...
Opens a generic WPF dialog window that displays a message and has a single OK button.