开发者

Drawing things on a Canvas

How would I draw something on a Canvas in C# for Windows Phone?

Okay, let me be a little more clear.

Say the user taps his finger down at 386,43 on the canvas. (the canvas is 768 by 480)

I would like my application to be able to respond by placing a red dot at 3开发者_如何转开发86,43 on the canvas.

I have no prior experience with Canvas whatsoever.

If this is too complex to be answered in one question (which it probably is), please give me links to other websites with Canvas and Drawing articles.


There are various ways of doing this. Depending on the nature of the red dot, you could make it a UserControl. For a basic circle, you can simply handle your canvas' ManipulationStarted event.

private void myCanvas_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{

            Ellipse el = new Ellipse();
            el.Width = 10;
            el.Height = 10;
            el.Fill = new SolidColorBrush(Colors.Red);
            Canvas.SetLeft(el, e.ManipulationOrigin.X);
            Canvas.SetTop(el, e.ManipulationOrigin.Y);
            myCanvas.Children.Add(el);
}


I think you need to approach the problem differently. (I'm not including code on purpose, because of that).

Forms and controls in an Windows applications (including Phone) can be refreshed for several reasons, at any time. If you draw on a canvas in response to a touch action, you have an updated canvas until the next refresh. If a refresh occurs the canvas repaints itself, you end up with a blank canvas.

I have no idea what your end goal is, but you likely want to either keep track of what the user has done and store that state somewhere and show it in a canvas on the repaint of the canvas. This could be done with storing all the actions and "replaying" them on the canvas, or simply storing the view of the canvas as a bitmap and reload the canvas with that bitmap when refreshed. But, in the later case I think using a canvas isn't the right solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜