私はリモートホスト上にWPF InkCanvasの描画を示すアプリケーションを開発しようとしています。基本的には、ローカルのInkCanvasを複数のリモートホストと同期させます。私はStrokesChanged
イベントにサブスクライブしました:2つのInkCanvas-es図面を同期させる方法は?
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
でカーブを消去するときにも、イベントは1回だけ呼び出され、リモートホストはthis.RemoteInkCanvas.Strokes.Remove(removedStrokes)
を正常に使用します。
ここで問題が発生します。
this.DrawingCanvas.EditingMode
がInkCanvasEditingMode.EraseByPoint
、イベントが一度呼び出されたが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);
}
}
例外は常にキャッチされています。
ここでの一般的な質問:コレクション内の同じストローク/ポイントを見つけてそれに応じて削除する方法は?あなたが複雑なこと、それを持っている必要がある場合
おかげで、しかし、私は別のコンピュータ上で私のInkCanvas-ESを持っています。 XAMLがネットワーク上のオブジェクトにバインドするソリューションはありません。 :) –