当我在 C# 中使用 inkcanvas1.Strokes.Erase 语句时,WPF-InkCanvas StrokeErased 事件未触发

WPF-InkCanvas StrokeErased event not firing when I used inkcanvas1.Strokes.Erase statement in C#

当我使用 inkcanvas1.EditingMode 时 InkCanvasEditingMode.EraseByPoint 它正在擦除并且还会触发 StrokeErased 事件。
但是当我通过 inkcanvas1.Strokes.Erase(); 擦除中风时函数然后 StrokeErased 事件不触发。
那我怎样才能识别哪些笔划被擦掉了,哪些笔画是新创建的呢?
考虑在我的水墨画布上千笔。
所以我无法维护每一个添加和删除的笔画。 有没有其他事件或解决方案。

我有下面的示例 WPF Inkcanvas 代码
XAML代码

<Window x:Class="MyWhiteBoard.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyWhiteBoard"
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <InkCanvas x:Name="inkcanvas1" Grid.Row="0" StrokeErased="inkcanvas1_StrokeErased" MouseMove="inkcanvas1_MouseMove"></InkCanvas>
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Width="100" Height="50" Background="WhiteSmoke" Content="Draw" Click="drawInkClick"></Button>
            <Button Width="100" Height="50" Background="WhiteSmoke" Content="Erase by Point" Click="EraseByPointsClick"></Button>
            <Button Width="100" Height="50" Background="WhiteSmoke" Content="Stroke Erase" Click="StrokeEraseClick"></Button>
            <Button Width="100" Height="50" Background="WhiteSmoke" Content="clear all" Click="clearAllClick"></Button>
        </StackPanel>
    </Grid>
</Window>

C# 代码背后的代码

public partial class MainWindow : Window
    {
        private bool IsStrokeEraseCalled = false;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void inkcanvas1_StrokeErased(object sender, RoutedEventArgs e)
        {
            // why this event is not calling when I use  
            //inkcanvas1.Strokes.Erase
        }
        private void EraseByPointsClick(object sender, RoutedEventArgs e)
        {
            inkcanvas1.EditingMode = InkCanvasEditingMode.EraseByPoint;
        }
        private void StrokeEraseClick(object sender, RoutedEventArgs e)
        {
            inkcanvas1.EditingMode = InkCanvasEditingMode.Select;
            IsStrokeEraseCalled = !IsStrokeEraseCalled;
        }
        private void clearAllClick(object sender, RoutedEventArgs e)
        {
            inkcanvas1.Strokes.Clear();
        }
        private void inkcanvas1_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsStrokeEraseCalled)
            {
                Point currentPoint = e.GetPosition((IInputElement)sender);
                List<Point> enumrator = new List<Point>();
                enumrator.Add(new Point(currentPoint.X, currentPoint.Y));
                StylusShape EraseShape = (StylusShape)new RectangleStylusShape(100, 100, 0);
                inkcanvas1.Strokes.Erase(enumrator, EraseShape);
            }
        }

        private void drawInkClick(object sender, RoutedEventArgs e)
        {
            inkcanvas1.EditingMode = InkCanvasEditingMode.Ink;
        }
    }

用以下代码替换您的 StrokeEraseClick 事件:

private void StrokeEraseClick(object sender, RoutedEventArgs e)
    {
        inkcanvas1.EditingMode = InkCanvasEditingMode.EraseByPoint;
        inkcanvas1.EraserShape = new RectangleStylusShape(100, 100);
        var editMode = inkcanvas1.EditingMode;
        inkcanvas1.EditingMode = InkCanvasEditingMode.None;
        inkcanvas1.EditingMode = editMode;
    }      

它会将当前模式保留为 InkCanvasEditingMode.EraseByPoint 并允许您擦除定义的区域描边。每次使用这种方法擦除笔划时,都会触发 StrokeErased 事件。

试试这个

  public MainWindow()
    {
        InitializeComponent();
        inkcanvas1.Strokes.StrokesChanged += Strokes_StrokesChanged;
    }

    private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
    {
        StrokeCollection newlyAddedStroke =  e.Added;
        StrokeCollection oldRemovedStroke = e.Removed;
    }
  1. 当您通过 inkcanvas1.Strokes.Erase(); 函数擦除笔画时,默认情况下 StrokeErased 事件 不会触发。
  2. 如果你想维护添加和删除的笔画,那么你可以在inkcanvas1.Strokes.StrokesChanged事件中处理它。
  3. 或者您应该处于 EraseByPoint 模式并使用语句
    inkcanvas1.EraserShape = new RectangleStylusShape(100, 100);
  4. 如果您的 inkcanvas 上有大量笔画,那么每次维护每个笔画集合都很繁重,那么您可以选择使用 ProperyData 属性.
  5. 为每个笔画添加唯一 ID