-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
77 lines (67 loc) · 1.66 KB
/
MainWindow.xaml.cs
File metadata and controls
77 lines (67 loc) · 1.66 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using SoftwarePatterns.Core.Momento;
namespace SoftwarePatterns.WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly Stack<IMemento> priorStates = new Stack<IMemento>();
public MainWindow()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo,
OnExectedCommands));
Canvas1.MouseUp += Canvas1OnMouseUp;
StoreState();
}
private void Canvas1OnMouseUp(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
StoreState();
}
private void OnExectedCommands(object sender, ExecutedRoutedEventArgs e)
{
var window = (MainWindow) sender;
if (e.Command == ApplicationCommands.Undo)
{
window.Undo(sender, e);
}
}
private void Undo(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
{
if (priorStates.Count > 1)
{
priorStates.Pop();
var lastState = priorStates.Peek();
Canvas1.SetMemento(lastState);
}
Label1.Content = priorStates.Count;
}
private void StoreState()
{
var memento = Canvas1.CreateMemento();
priorStates.Push(memento);
Label1.Content = priorStates.Count;
}
}
public class InkCanvasWithUndo : InkCanvas
{
public IMemento CreateMemento()
{
var copy = Strokes.ToArray();
return new InkCanvasMemeto { State = copy };
}
public void SetMemento(IMemento memento)
{
Strokes = new StrokeCollection((Stroke[])memento.State);
}
}
}