File size: 5,446 Bytes
fab29d7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
using Microsoft.Win32;
namespace VersOne.Epub.WpfDemo.WpfEnvironment
{
internal class WindowManager : IWindowManager
{
private static readonly WindowManager instance;
private readonly Dictionary<string, WindowInfo> registeredWindowsByViewName;
private readonly Dictionary<Type, WindowInfo> registeredWindowsByViewModel;
private readonly Dictionary<string, IWindowContext> openWindows;
private IWindowContext lastActivatedWindowContext;
static WindowManager()
{
instance = new WindowManager();
}
private WindowManager()
{
registeredWindowsByViewName = new Dictionary<string, WindowInfo>();
registeredWindowsByViewModel = new Dictionary<Type, WindowInfo>();
openWindows = new Dictionary<string, IWindowContext>();
lastActivatedWindowContext = null;
EnumerateWindowsInAssembly();
}
public static IWindowManager Instance
{
get
{
return instance;
}
}
public IWindowContext CreateWindow(object viewModel)
{
if (viewModel == null)
{
throw new ArgumentNullException("viewModel");
}
if (!registeredWindowsByViewModel.TryGetValue(viewModel.GetType(), out WindowInfo windowInfo))
{
throw new ArgumentException($"There are no registered window for {viewModel.GetType().FullName} type.");
}
if (!(Activator.CreateInstance(windowInfo.WindowType) is Window window))
{
throw new InvalidOperationException($"There was an error while trying to create an instance of {windowInfo.WindowType.FullName} window class.");
}
window.DataContext = viewModel;
IWindowContext windowContext = new WindowContext(this, windowInfo.ViewName, window, viewModel);
windowContext.Activated += Activated;
windowContext.Showing += Showing;
windowContext.Closed += Closed;
return windowContext;
}
public IWindowContext FindActiveWindow()
{
return lastActivatedWindowContext;
}
public OpenFileDialogResult ShowOpenFileDialog(OpenFileDialogParameters openFileDialogParameters)
{
if (openFileDialogParameters == null)
{
throw new ArgumentNullException("openFileDialogParameters");
}
OpenFileDialog openFileDialog = new OpenFileDialog();
if (!String.IsNullOrEmpty(openFileDialogParameters.Filter))
{
openFileDialog.Filter = openFileDialogParameters.Filter;
}
openFileDialog.Multiselect = openFileDialogParameters.Multiselect;
if (!String.IsNullOrEmpty(openFileDialogParameters.InitialDirectory))
{
openFileDialog.InitialDirectory = openFileDialogParameters.InitialDirectory;
}
bool showDialogResult = openFileDialog.ShowDialog() == true;
return new OpenFileDialogResult
{
DialogResult = showDialogResult,
SelectedFilePaths = showDialogResult ? openFileDialog.FileNames.ToList() : new List<string>()
};
}
private void EnumerateWindowsInAssembly()
{
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
{
if (type.Name.EndsWith("View", StringComparison.OrdinalIgnoreCase) && typeof(Window).IsAssignableFrom(type))
{
RegisterWindow(type);
}
}
}
private void RegisterWindow(Type windowType)
{
string viewName = windowType.Name.Substring(0, windowType.Name.Length - 4);
if (registeredWindowsByViewName.ContainsKey(viewName))
{
throw new InvalidOperationException($"View {viewName} has been declared more than once.");
}
string viewModelTypeName = viewName + "ViewModel";
Type viewModelType = Array.Find(Assembly.GetExecutingAssembly().GetTypes(), type => String.Compare(type.Name, viewModelTypeName, StringComparison.OrdinalIgnoreCase) == 0);
WindowInfo windowInfo = new WindowInfo(viewName, windowType, viewModelType);
registeredWindowsByViewName.Add(viewName, windowInfo);
if (viewModelType != null)
{
registeredWindowsByViewModel.Add(viewModelType, windowInfo);
}
}
private void Activated(object sender, EventArgs e)
{
lastActivatedWindowContext = (IWindowContext)sender;
}
private void Showing(object sender, EventArgs e)
{
IWindowContext windowContext = (IWindowContext)sender;
openWindows.Add(windowContext.ViewName, windowContext);
}
private void Closed(object sender, EventArgs e)
{
IWindowContext windowContext = (IWindowContext)sender;
openWindows.Remove(windowContext.ViewName);
windowContext.Showing -= Showing;
windowContext.Closed -= Closed;
}
}
}
|