在 Silverlight 中使用 TransformToVisual 定位 HTML 元素

发布于 2024-07-18 03:18:22 字数 1712 浏览 3 评论 0原文

我正在开发一个“托管”Flash 实例的 Silverlight 自定义控件。 当然,执行此操作的方法是将相关 HTML 元素放置在 Silverlight 实例上,如所述,例如 此处。 我遇到的问题是,当我使用 GeneralTransform.Transform() 方法获取控件的绝对坐标,以便我可以正确定位 HTML 元素时,返回的 Point 对象始终具有 .X=0 并且.Y=0。

public void InitControl(string id)
{
    GeneralTransform gt = this.TransformToVisual(Application.Current.RootVisual);
    Point localPos = gt.Transform(new Point(_htmlControlLeft, _htmlControlTop));

    // Create the containing DIV tag.
    HtmlDocument doc = HtmlPage.Document;
    divHost = doc.CreateElement("div");
    divHost.SetAttribute("id", System.Guid.NewGuid().ToString());
    divHost.SetStyleAttribute("position", "absolute");
    divHost.SetStyleAttribute("left", localPos.X.ToString() + "px"); // always 0
    divHost.SetStyleAttribute("top", localPos.Y.ToString() + "px"); // always 0
    Debug.WriteLine("x,y=" + localPos.X.ToString() + "," + localPos.Y.ToString());
    divHost.SetStyleAttribute("width", Width.ToString() + "px");
    divHost.SetStyleAttribute("height", Height.ToString() + "px");
    divHost.SetStyleAttribute("z-index", _htmlZIndex.ToString());
}

我无法找到有关 GeneralTransform.Transform() 方法的详细文档,但似乎我正确使用了它。 对我做错了什么有什么想法吗?

编辑 2009 年 4 月 28 日:我仍然没有找到答案,但我正在正确使用 Transform() 方法。 仅当我在 Page.Loaded 事件期间调用 InitControl() 方法时,该问题才会出现。 如果我等待几秒钟,然后从 Button_Click 事件中调用它(例如),相同的代码可以正常工作。 根据 SL 文档,在 Page.Loaded 事件触发时,所有内容都应该正确布局,但显然情况并非如此。

我还应该注意到,每隔一段时间,上面的代码就可以完美地工作,即使是从 Page.Loaded 事件调用它时也是如此。 呵呵。

到目前为止,我的解决方法是在表单加载后隐藏控件几秒钟,然后显示它。 这是一个丑陋的黑客,但除非有人有更好的想法......?

I'm in the process of developing a Silverlight custom control that "hosts" a Flash instance. The way that you do this, of course, is to position the HTML element in question over your Silverlight instance, as described, say, here. The problem I'm running into is that when I use the GeneralTransform.Transform() method to get the absolute coordinates of my control, so that I can position the HTML element correctly, the Point object that's returned always has .X=0 and .Y=0.

public void InitControl(string id)
{
    GeneralTransform gt = this.TransformToVisual(Application.Current.RootVisual);
    Point localPos = gt.Transform(new Point(_htmlControlLeft, _htmlControlTop));

    // Create the containing DIV tag.
    HtmlDocument doc = HtmlPage.Document;
    divHost = doc.CreateElement("div");
    divHost.SetAttribute("id", System.Guid.NewGuid().ToString());
    divHost.SetStyleAttribute("position", "absolute");
    divHost.SetStyleAttribute("left", localPos.X.ToString() + "px"); // always 0
    divHost.SetStyleAttribute("top", localPos.Y.ToString() + "px"); // always 0
    Debug.WriteLine("x,y=" + localPos.X.ToString() + "," + localPos.Y.ToString());
    divHost.SetStyleAttribute("width", Width.ToString() + "px");
    divHost.SetStyleAttribute("height", Height.ToString() + "px");
    divHost.SetStyleAttribute("z-index", _htmlZIndex.ToString());
}

I haven't been able to find great documentation on the GeneralTransform.Transform() method, but it seems like I'm using it correctly. Any thoughts on what I'm doing wrong?

Edit 4/28/09: I still haven't found an answer, but I AM using the Transform() method properly. The problem only shows up if I call the InitControl() method during the Page.Loaded event. If I wait a few seconds, then call it (say) from a Button_Click event, the same code works fine. According to the SL docs, everything should be laid out appropriately by the time the Page.Loaded event fires, but clearly that's not the case.

I should also note that every once in a while, the code above works perfectly fine, even when it's called from the Page.Loaded event. Huh.

My workaround so far, for what it's worth, is hide the control for a couple seconds after the form loads, then show it. It's an ugly hack, but unless anyone has any better ideas...?

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

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

发布评论

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

评论(1

眼波传意 2024-07-25 03:18:22

这是由于 RootVisual 具有 0,0 x/y 轴。 尝试使用树中的下一个元素,通常默认情况下是layoutRoot(假设您没有更改其名称等)。 或者利用 FindName() 确保您可以直接引用您想要将 iFrame 放置在其上的所述图层。

Scott Barnes / 丰富平台产品经理 / Microsoft。

This is due to RootVisual having a 0,0 x/y axis. Try using the next element within the tree, typically it's layoutRoot by default (assuming you haven't changed its name etc). That or utilise FindName() to ensure you have a direct reference to the said layer you are wanting to position the iFrame over.

Scott Barnes / Rich Platforms Product Manager / Microsoft.

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