リモートホストでWPFInkCanvasの描画を表示するアプリケーションを開発しようとしています。基本的に、ローカルInkCanvasを複数のリモートホストと同期します。私はイベントを購読しStrokesChanged
ました:
this.DrawingCanvas.Strokes.StrokesChanged += this.Strokes_StrokesChanged;
そしてハンドラー。
private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
{
if (e.Added != null && e.Added.Count > 0)
{
this.StrokeSynchronizer.SendAddedStroke(e.Added);
}
if (e.Removed != null && e.Removed.Count > 0)
{
this.StrokeSynchronizer.SendRemovedStroke(e.Removed);
}
}
新しい曲線を描画しているとき、イベントは1回だけ呼び出されます。リモートホストは、を呼び出すことによってそれを正しく描画しthis.RemoteInkCanvas.Strokes.Add(addedStrokes)
ます。
イベントを介してカーブを消去しているときInkCanvasEditingMode.EraseByStroke
も一度呼び出され、リモートホストはthis.RemoteInkCanvas.Strokes.Remove(removedStrokes)
正常に使用します。
ここに問題があります!
this.DrawingCanvas.EditingMode
の場合、InkCanvasEditingMode.EraseByPoint
イベントは1回呼び出されますが、2つのコレクション(追加と削除)があります。これにより、リモートホストが異常になります。ストロークを消去するリモートホストコードは次のとおりです。
private StrokeCollection FindStrokesInLocalCollection(StrokeCollection receivedCollection)
{
var localStrokes = new StrokeCollection();
foreach (var erasedStroke in receivedCollection)
{
var erasedPoints = erasedStroke.StylusPoints;
foreach (var existentStoke in this.RemoteInkCanvas.Strokes)
{
var existentPoints = existentStoke.StylusPoints;
if (erasedPoints.SequenceEqual(existentPoints))
{
localStrokes.Add(existentStoke);
}
}
}
return localStrokes;
}
private void RemoteStrokeRemoved(StrokeCollection strokes)
{
try
{
// Simple this.RemoteInkCanvas.Strokes.Remove(strokes)
// does not work, because local and remote strokes are different (though equal) objects.
// Thus we need to find same strokes in local collection.
var strokesToRemove = this.FindStrokesInLocalCollection(strokes);
if (strokesToRemove.Count != strokes.Count)
{
Logger.Warn(string.Format(
"Whiteboard: Seems like remotely removed strokes were not found in local whiteboard. Remote count {0}, local count {1}.",
strokes.Count,
strokesToRemove.Count));
}
this.RemoteInkCanvas.Strokes.Remove(strokesToRemove);
}
catch (Exception ex)
{
Logger.Error("Whiteboard: Can not remove some strokes received from remote host.", ex);
}
}
例外は常にキャッチされていることに注意してください。
ここでの一般的な質問:コレクション内で同じストローク/ポイントを見つけて、それに応じてそれらを削除するにはどうすればよいですか?