在画布上画东西

发布于 2024-11-07 04:36:03 字数 306 浏览 1 评论 0原文

如何使用 C# 在 Windows Phone 的 Canvas 上绘制内容?

好吧,让我说得更清楚一些。

假设用户用手指在画布上点击 386,43。 (画布尺寸为 768 x 480)

我希望我的应用程序能够通过在画布上的 386,43 处放置一个红点来做出响应。

我之前没有使用 Canvas 的任何经验。

如果这个问题太复杂而无法在一个问题中回答(很可能是这样),给我提供包含 Canvas 和 Drawing 文章的其他网站的链接。

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 386,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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阪姬 2024-11-14 04:36:03

有多种方法可以做到这一点。根据红点的性质,您可以将其设为用户控件。对于基本圆,您只需处理画布的 ManipulationStarted 事件即可。

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);
}

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);
}
不气馁 2024-11-14 04:36:03

我认为你需要以不同的方式处理这个问题。 (因此,我没有故意包含代码)。

Windows 应用程序(包括 Phone)中的表单和控件可能会因多种原因而随时刷新。如果您在画布上绘制以响应触摸操作,则在下次刷新之前您将拥有更新的画布。如果发生刷新,画布会重新绘制自身,您最终会得到一个空白画布。

我不知道你的最终目标是什么,但你可能想要跟踪用户所做的事情并将该状态存储在某处,并在画布的重绘上将其显示在画布中。这可以通过存储所有操作并在画布上“重播”它们,或者简单地将画布视图存储为位图并在刷新时使用该位图重新加载画布来完成。但是,在后一种情况下,我认为使用画布不是正确的解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文