bovender framework
C# framework that implements MVVM and more
ShowViewAction.cs
1 /* ShowViewAction.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.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using System.Windows;
23 using Bovender.Mvvm.Messaging;
24 using Bovender.Extensions;
25 
26 namespace Bovender.Mvvm.Actions
27 {
33  {
34  #region Public properties
35 
36  public string Assembly { get; set; }
37  public string View { get; set; }
38 
39  #endregion
40 
41  #region Overrides
42 
43  protected override Window CreateView()
44  {
45  object obj = Activator.CreateInstance(Assembly, View).Unwrap();
46  Window view = obj as Window;
47  if (view != null)
48  {
49  Logger.Info("CreateView: Created {0}", view.GetType().FullName);
50  return view;
51  }
52  else
53  {
54  Logger.Fatal("Class {0} in assembly {1} is not derived from Window", Assembly, View);
55  throw new ArgumentException(String.Format(
56  "Class '{0}' in assembly '{1}' is not derived from Window.",
57  Assembly, View));
58  }
59  }
60 
61  protected override void ShowView(Window view)
62  {
63  view.ShowInForm();
64  }
65 
66  protected override ViewModels.ViewModelBase GetDataContext(MessageContent messageContent)
67  {
68  ViewModelMessageContent vmc = messageContent as ViewModelMessageContent;
69  if (vmc != null)
70  {
71  return vmc.ViewModel;
72  }
73  else
74  {
75  Logger.Fatal("GetDataContext: Expected ViewModelMessageContent, got {0}",
76  messageContent == null ? "null" : messageContent.GetType().FullName);
77  throw new ArgumentException("Invalid message content: ShowViewAction requires ViewModelMessageContent");
78  }
79  }
80 
81  #endregion
82 
83  #region Class logger
84 
85  private static NLog.Logger Logger { get { return _logger.Value; } }
86 
87  private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
88 
89  #endregion
90  }
91 }
Injects a view with a view model that is referenced in a ViewModelMessageContent, and shows the view ...
Message content that holds a reference to a view model.
override ViewModels.ViewModelBase GetDataContext(MessageContent messageContent)
Gets a ViewModelBase object that will be injected into the view In the abstract base class...
override void ShowView(Window view)
Shows the view as a modal dialog.
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.