gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:30:30 26 4
gpt4 key购买 nike

当我使用inkcanvas1.EditingMode 是InkCanvasEditingMode.EraseByPoint 它正在删除并且StrokeErased 事件触发。
但是当我通过inkcanvas1.Strokes.Erase()删除笔画时;函数然后 StrokeErased 事件不会触发。那么我如何识别哪些笔画被删除,哪些笔画是新创建的。考虑在我的墨水 Canvas 上数千笔画。所以我无法保持每一个添加和删除的笔画。
有没有其他事件或任何解决方案。
我有以下示例 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;
}
}

最佳答案

尝试这个

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

private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
{
StrokeCollection newlyAddedStroke = e.Added;
StrokeCollection oldRemovedStroke = e.Removed;
}

关于c# - 当我在 C# 中使用inkcanvas1.Strokes.Erase 语句时,WPF-InkCanvas StrokeErased 事件不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64486215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com