I have a canvas, on which freehand drawing can be done (similar to this http://www.windowsphonegeek.com/tips/drawing-in-wp7-2-drawing-shapes-with-finger). My requirement is, I have to make the drawing play and pause programaticaly. There is but开发者_JAVA技巧ton, on click the drawing should be played the way it was drawn. Please guide me what steps should be taken to acheive this, also if there is any sample application or link is available. Thanks.
Update: I finally got around to fixing this so it works in Windows Phone 7, and not just WPF.
Based on the example you cited.
Define a list of Points:
private List<Point> _points;
When the LeftMouseButton is Down AND Up add a blank point and the starting point:
oldPoint = currentPoint;
_points.Add(new Point(-1, -1));
_points.Add(oldPoint);
Your Play button event handler will look like this:
canvas.Children.Clear();
// Use the BackgroundWorker to draw the lines
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync();
The worker_DoWork iterates through _points and sends them on to our dispatcher:
void worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int ix = 0; ix < _points.Count - 1; ix++)
{
Point start = _points[ix];
Point end = _points[ix + 1];
if (start.X == -1 || end.X == -1)
continue;
Thread.Sleep(10);
DoEvents(start, end);
}
}
DoEvents uses the Dispatcher
to send the line coordinates to a delegated method
private delegate void AddLineDelegate(Point p1, Point p2);
private void DoEvents(Point p1, Point p2)
{
this.Dispatcher.BeginInvoke(new AddLineDelegate(AddLine), new object[] { p1, p2 });
}
private void AddLine(Point p1, Point p2)
{
Line line = new Line() { X1 = p1.X, Y1 = p1.Y, X2 = p2.X, Y2 = p2.Y };
line.Stroke = new SolidColorBrush(Colors.Black);
line.StrokeThickness = 4;
this.canvas.Children.Add(line);
}
精彩评论