File size: 4,100 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.ComponentModel;
using System.Windows;
using System.Windows.Interop;
namespace VersOne.Epub.WpfDemo.WpfEnvironment
{
internal class WindowContext : IWindowContext
{
private readonly IWindowManager windowManager;
internal WindowContext(IWindowManager windowManager, string viewName, Window window, object dataContext)
{
this.windowManager = windowManager;
ViewName = viewName;
Window = window;
DataContext = dataContext;
Window.Activated += Window_Activated;
Window.Closing += Window_Closing;
Window.Closed += Window_Closed;
}
public event EventHandler Activated;
public event EventHandler Showing;
public event EventHandler Closing;
public event EventHandler Closed;
public string ViewName { get; private set; }
public Window Window { get; private set; }
public object DataContext { get; private set; }
public void Show(bool showMaximized = false)
{
if (!Window.IsVisible)
{
OnShowing();
Window.WindowState = GetWindowState(showMaximized);
Window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
Window.Show();
}
else
{
if (Window.WindowState == WindowState.Minimized)
{
Window.WindowState = WindowState.Normal;
}
else
{
Window.Activate();
}
}
}
public bool? ShowDialog(IWindowContext ownerWindowContext = null, bool showMaximized = false)
{
OnShowing();
if (ownerWindowContext == null)
{
ownerWindowContext = windowManager.FindActiveWindow();
}
Window.Owner = ownerWindowContext.Window;
return ShowDialog(showMaximized, true);
}
public bool? ShowDialog(IntPtr ownerHandle, bool showMaximized = false)
{
OnShowing();
WindowInteropHelper windowInteropHelper = new WindowInteropHelper(Window);
windowInteropHelper.Owner = ownerHandle;
return ShowDialog(showMaximized, ownerHandle != IntPtr.Zero);
}
public void Close()
{
Window.Close();
}
public void CloseDialog(bool dialogResult)
{
Window.DialogResult = dialogResult;
}
public void Focus()
{
Window.Focus();
}
protected virtual void OnActivated()
{
Activated?.Invoke(this, EventArgs.Empty);
}
protected virtual void OnShowing()
{
Showing?.Invoke(this, EventArgs.Empty);
}
protected virtual void OnClosing()
{
Closing?.Invoke(this, EventArgs.Empty);
}
protected virtual void OnClosed()
{
Closed?.Invoke(this, EventArgs.Empty);
}
private bool? ShowDialog(bool showMaximized, bool hasOwner)
{
Window.WindowStartupLocation = hasOwner ? WindowStartupLocation.CenterOwner : WindowStartupLocation.CenterScreen;
Window.ShowInTaskbar = false;
Window.WindowState = GetWindowState(showMaximized);
return Window.ShowDialog();
}
private WindowState GetWindowState(bool isMaximized)
{
return isMaximized ? WindowState.Maximized : WindowState.Normal;
}
private void Window_Activated(object sender, EventArgs e)
{
OnActivated();
}
private void Window_Closing(object sender, CancelEventArgs e)
{
OnClosing();
}
private void Window_Closed(object sender, EventArgs e)
{
OnClosed();
Window.Closing -= Window_Closing;
Window.Closed -= Window_Closed;
}
}
}
|