我想實作inkcanvas筆畫的撤消和重做。
我想實作可以連續多次操作的重做和撤消。
我不知道我的代碼哪里出了問題。請幫我。
我的代碼如下:
xml:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<InkCanvas x:Name="inkCanvas"
Grid.Column="0" Grid.ColumnSpan="2"
Grid.Row="0" Grid.RowSpan="7"
Width="Auto" Height="Auto" EditingMode="Ink"
IsHitTestVisible="True"
Background="LightSeaGreen"
UseCustomCursor="True"
Cursor="Pen"/>
<Button x:Name="btn_Test1"
Grid.Row="0"
Grid.Column="1"
Width="100"
Height="50"
Content="pen"
Cursor="Hand"
Tag="Test1"
Click="Button_Click" />
<Button x:Name="btn_Test3"
Grid.Row="1"
Grid.Column="1"
Width="100"
Height="50"
Content="clear"
Tag="Test3"
Click="Button_Click" />
<Button x:Name="btn_Test4"
Grid.Row="2"
Grid.Column="1"
Width="100"
Height="50"
Content="UnDo"
Tag="Undo"
Click="Button_Click" />
<Button x:Name="btn_Test5"
Grid.Row="3"
Grid.Column="1"
Width="100"
Height="50"
Content="ReDo"
Tag="Redo"
Click="Button_Click" />
</Grid>
后面的代碼:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Media;
namespace InkCanvasUndoRedo
{
public partial class MainWindow : Window
{
public Stack<DoStroke> DoStrokes { get; set; }
public Stack<DoStroke> UndoStrokes { get; set; }
private bool handle = true;
public MainWindow()
{
InitializeComponent();
DoStrokes = new Stack<DoStroke>();
UndoStrokes = new Stack<DoStroke>();
inkCanvas.DefaultDrawingAttributes.FitToCurve = true;
inkCanvas.DefaultDrawingAttributes.Color = Color.FromArgb(255, 255, 255, 255);
inkCanvas.Strokes.StrokesChanged = Strokes_StrokesChanged;
}
private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
{
if (handle)
{
DoStrokes.Push(new DoStroke
{
ActionFlag = e.Added.Count > 0 ? "ADD" : "REMOVE",
Stroke = e.Added.Count > 0 ? e.Added[0] : e.Removed[0]
});
}
}
public void Undo()
{
handle = false;
if (DoStrokes.Count > 0)
{
DoStroke dos = DoStrokes.Pop();
if (dos.ActionFlag.Equals("ADD"))
{
inkCanvas.Strokes.Remove(dos.Stroke);
}
else
{
inkCanvas.Strokes.Add(dos.Stroke);
}
UndoStrokes.Push(dos);
}
handle = true;
}
public void Redo()
{
handle = false;
if (UndoStrokes.Count > 0)
{
DoStroke dos = UndoStrokes.Pop();
if (dos.ActionFlag.Equals("ADD"))
{
inkCanvas.Strokes.Add(dos.Stroke);
}
else
{
inkCanvas.Strokes.Remove(dos.Stroke);
}
}
handle = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
switch ((sender as Button).Tag)
{
case "Test1":
inkCanvas.EditingMode = InkCanvasEditingMode.Ink;
break;
case "Test3":
inkCanvas.Strokes.Clear();
break;
case "Undo":
Undo();
break;
case "Redo":
Redo();
break;
}
}
}
public struct DoStroke
{
public string ActionFlag { get; set; }
public Stroke Stroke { get; set; }
}
}
結果: 寫: 123
點擊: undo->undo->redo->redo->undo
預期:3消失
實際:1 消失

uj5u.com熱心網友回復:
在你的重做方法中你做
DoStroke dos = UndoStrokes.Pop();
if (dos.ActionFlag.Equals("ADD"))
{
inkCanvas.Strokes.Add(dos.Stroke);
}
else
{
inkCanvas.Strokes.Remove(dos.Stroke);
}
您可能應該DoStrokes.Push(dos);在此結束時致電。以與撤消方法中類似的方式進行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/425995.html
