相当于绘图应用程序的橡皮擦、c#、silverlight、wp7
我正在开发一个简单的绘图应用程序,以进一步提高我的技能,但我似乎无法理解橡皮擦工具的逻辑。该应用程序仅使用 Line 类在用户移动手指时创建线条。对于橡皮擦工具,我尝试使用 VisualTreeHelper,如下所示:
List<UIElement> elements = (List<UIElement>)VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(tree), ContentPanelCanvas);
foreach (UIElement element in elements)
{
if (element is Line)
{
this.ContentPanelCanvas.Children.Remove(element);
}
}
它在某些时候可能非常缓慢且滞后。有时我必须触摸该区域超过 5 次才能摆脱那里的线。
有替代方案吗?
I'm working on a simple drawing app to further advance my skills and I can't seem to get the logic down for an eraser tool. The app simply uses the Line class to create lines as the user moves their finger. For the eraser tool I tried using the VisualTreeHelper as follows:
List<UIElement> elements = (List<UIElement>)VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(tree), ContentPanelCanvas);
foreach (UIElement element in elements)
{
if (element is Line)
{
this.ContentPanelCanvas.Children.Remove(element);
}
}
It at some points but can be very slow and laggy. Sometimes I would have to touch the area more than 5 times to get rid of the line there.
Is there an alternative to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
e.GetPosition(tree)
将返回一个点。尝试使用以位置为中心的矩形
。请注意,不要将 FindElementsInHostCooperatives 的结果强制转换为
List
,这是一个实现细节,文档仅保证它是一个IEnumerable
,除此之外这是不必要的演员阵容。The
e.GetPosition(tree)
will be returning a point. Try instead using aRect
with the position as its center.Note don't cast the result of FindElementsInHostCoordinates to
List<T>
, that is an implementation detail, the documentation only guarantees it to be anIEnumerable<UIElement>
, besides which it is an unnecessary cast.您实际上正在寻找与单个像素的命中测试相匹配的元素集。如果你的线条很窄,那么它就像大海捞针;很难精确地击中线将其移除。
相反,您需要使用矩形而不是点来进行模糊匹配。您可以使用相同的 API,只是其矩形版本:
You are actually looking for the set of elements that match the hit test of a single pixel. If your lines are narrow, then it's like a needle in a haystack; it's very hard to hit the line precisely to remove it.
Instead you need to use a fuzzy match using a rectangle instead of a point. You can use the same API, just the rectangle version of it:
VisualTreeHelper.FindElementsInHostCoordinates(r, MainCanvas);
不返回任何元素。
VisualTreeHelper.FindElementsInHostCoordinates(r, MainCanvas);
Is not returning any Elements.