WebBrowser控件而getElement通过ID [英] WebBrowser Control and GetElement by ID

查看:192
本文介绍了WebBrowser控件而getElement通过ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual C#的WinForms来控制WebBrowser对象。

I am using Visual C# Winforms to control a WebBrowser object.

我特别想用WebBrowser.Document.GetObjectByID(身份识别码)。样式设置一个对象,它是加载到WebBrowser对象的文档的一部分的风格。

Specifically I want to use WebBrowser.Document.GetObjectByID("myid").Style to set the style of an object that is part of the document loaded into the WebBrowser object.

我想有按钮在WinForm切换标题的风格从显示:无到显示:块;文本对齐:中间。
这是我在做什么:

I want to have button on the WinForm toggle the Style of a heading from "display:none" to "display:block;text-align:middle". This is what I am doing:

private void frmView_Load(object sender, EventArgs e)
{
string question = "How many cows?";
string answer = "5 cows";
webBrowser1.Navigate("about:blank");
webBrowser1.Document.OpenNew(false);
webBrowser1.Document.Write("<html><body><div id='question'><h1>");
webBrowser1.Document.Write(question);
webBrowser1.Document.Write("</h1></div><div id='answer'><h2>");
webBrowser1.Document.Write(answer);
webBrowser1.Document.Write("</h2></div></body></html>");
webBrowser1.Refresh();
webBrowser1.Document.GetElementById("answer").Style = "display:none;";
//if I do a breakpoint here, I get the Style = "DISPLAY:NONE"
btnAnswer.Visible = true;
btnNext.Visible = true;
}

private void btnAnswer_Click(object sender, EventArgs e)
{
//if this is the first time ran, doing a breakpoint here will
//show Style to be NULL
webBrowser1.Document.GetElementById("answer").Style = "display:block;text-align:center";
//now the Style is properly set, and will remain that way, even after this function returns}

请注意,我能够控制的btnAnswer_Click方法Style属性,但不能与frmView_Load方法。我知道我可以只是把样式信息到div标签,当我创建它(这样做的工作),但为什么不code以上工作?

Note that I am able to control the Style property with the btnAnswer_Click method, but not with the frmView_Load method. I know I can just put the style info into div tag when I create it (this does work), but why doesn't the code above work?

更新:

我发现了一些可能在正确的方向走了。如果我称之为 webBrowser1.Update()在后 WebBrowser1.Navigate时任一点(关于:空白)一切frmView_Load工作。不幸的是,任何企图改变后frmView_Load出口将失败的文档。他们不仅将失败,但frmView_Load返回后webBrowser1.Document将被设置为NULL莫名其妙。
任何人都可以提供一些线索对这个控制是如何工作的?

Update:
I found something that may be going in the right direction. If I call webBrowser1.Update() at any point after webBrowser1.Navigate("about:blank") everything in frmView_Load works. Unfortunately, any attempts to change the document after frmView_Load exits will fail. Not only will they fail, but after frmView_Load returns webBrowser1.Document will be set to NULL somehow. Can anyone shed some light on how this control is supposed to work?

推荐答案

检讨WebBrowser控件MS文档后,我意识到,一切浏览器是异步的。话虽如此,这意味着我必须为了确保这些属性已经设置设置属性来阻止​​我的控制线程。

After reviewing the MS Documentation on the WebBrowser control, I realize that everything the browser does is asynchronous. Having said that, it means that I have to block my control threads after setting properties in order to ensure those properties have been set.

基本上,我的写到文档不这样做只是因为写函数返回。我必须阻止控制线程,直到文件反映了这些变化,如果我想引用写的内容。 MS建议睡眠循环。下面是我写&安培;块的功能如下:

Basically, my "write" to the document isn't done just because the "write" function returns. I have to block the control thread till the document reflects those changes if I want to reference the contents of that write. MS recommends a sleep loop. Here's what my write & block function looks like:

private void ChangeDocument(string documentText, double timeout)
{
    DateTime startTime = DateTime.Now;
    double elapsed = 0;

    if (webBrowser1.Document == null)
    {
        webBrowser1.Navigate("about:blank");
    }

    webBrowser1.Document.OpenNew(false);

    while ((webBrowser1.DocumentText != "") && (elapsed < timeout))
    {
        Thread.Sleep(50);
        elapsed = DateTime.Now.Subtract(startTime).TotalMilliseconds;
    }

    webBrowser1.Document.Write(documentText);

    startTime = DateTime.Now;
    elapsed = 0;

    while ((webBrowser1.DocumentText != documentText) && (elapsed < timeout))
    {
        System.Threading.Thread.Sleep(50);
        elapsed = DateTime.Now.Subtract(startTime).TotalMilliseconds;
    }
}

感谢您的答复大家。

Thanks for your responses everyone.

这篇关于WebBrowser控件而getElement通过ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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