你如何在WP7上的silverlight中找到控件的屏幕位置? [英] How do you find the screen position of a control in silverlight on WP7?

查看:22
本文介绍了你如何在WP7上的silverlight中找到控件的屏幕位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何在 WP7 上的 Silverlight 中找到控件的屏幕位置?

How do you find the screen position of a control in silverlight on WP7?

非常感谢任何建议.

推荐答案

如果你知道它是如何定位的(例如在 Canvas 中),可能会有更合适的做事方式,但总的来说,您可以使用 UIElement.TransformToVisual 在控件和全局坐标系之间进行转换:

If you know how it's positioned (eg in a Canvas) there may be a more appropriate way to do things, but in general, you can use UIElement.TransformToVisual to convert between the control's and the global coordinate system:

var transform = myControl.TransformToVisual(Application.Current.RootVisual)
var offset = transform.Transform(new Point(0,0))

在尝试执行此操作之前,请确保控件已经过布局,如果您过早地进行布局,错误消息通常无济于事.

Be sure that the control has undergone layout before trying to do this, if you do it too early the error messages are generally unhelpful.

(MSDN 建议该方法存在于 WP7 中,我没有安装 SDK 进行测试)

(MSDN suggests the method is present in WP7, I don't have the SDK installed to test)

这篇关于你如何在WP7上的silverlight中找到控件的屏幕位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆