点击DotNetBrowser

时间:2017-04-29 17:34:40

标签: javascript c# dotnetbrowser

我尝试使用具有特定html控件的DotNetBrowser控件打开一个Windows窗体,如下面的代码。

点击html后按钮,我需要隐藏加载的表单,然后显示第二个窗体。

我使用了如下的c#代码:

public partial class Form1 : Form
{
    private Browser browser;
    public Form1()
    {
        InitializeComponent();
        browser = BrowserFactory.Create();
        browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
        {
            if (e.IsMainFrame)
            {
                JSValue value = browser.ExecuteJavaScriptAndReturnValue("window");
                value.AsObject().SetProperty("Account", new Form1());
            }
        };

        browser.LoadHTML(@"<!DOCTYPE html>
                                <html>
                                <head>
                                    <script type='text/javascript'>
                                          function sendFormData() {
                                            var firstNameValue = myForm.elements['firstName'].value;
                                            var lastNameValue = myForm.elements['lastName'].value;
                                            // Invoke the 'save' method of the 'Account' Java object.
                                            Account.Save(firstNameValue, lastNameValue);
                                          }
                                        </script>
                                </head>
                                <body>
                                <form name='myForm'>
                                    First name: <input type='text' name='firstName'/>
                                    <br/>
                                    Last name: <input type='text' name='lastName'/>
                                    <br/>
                                    <input type='button' value='Save' onclick='sendFormData();'/>
                                </form>
                                </body>
                                </html>");


        BrowserView browserView = new WinFormsBrowserView(browser);
        this.Controls.Add((Control)browserView.GetComponent());
    }

    public void Save(String firstName, String lastName)
    {
        string _firstname = firstName;            

        this.Hide();
        SecondForm frm = new SecondForm(firstName);
        frm.ShowDialog();

    }

问题是,第一种形式(持有浏览器控件)不会隐藏并仍然聚焦。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

要对DotNetBrowser事件执行任何与UI相关的操作,必须将执行传递给主UI线程。在其他情况下,某些方法会抛出跨线程操作异常,甚至在没有任何消息的情况下失败。

要从JavaScript隐藏.NET回调中的表单,您需要将对Hide方法的调用包装到BeginInvoke()中。修改后的示例代码如下所示:

public partial class Form1 : Form
{
    private Browser browser;
    public Form1()
    {
        InitializeComponent();
        browser = BrowserFactory.Create();
        browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e)
        {
            if (e.IsMainFrame)
            {
                JSValue value = browser.ExecuteJavaScriptAndReturnValue("window");
                value.AsObject().SetProperty("Account", new Account(this));
            }
        };
        browser.LoadHTML(@"<!DOCTYPE html>
                            <html>
                            <head>
                                <script type='text/javascript'>
                                        function sendFormData() {
                                        var firstNameValue = myForm.elements['firstName'].value;
                                        var lastNameValue = myForm.elements['lastName'].value;
                                        // Invoke the 'save' method of the 'Account' Java object.
                                        Account.Save(firstNameValue, lastNameValue);
                                        }
                                    </script>
                            </head>
                            <body>
                            <form name='myForm'>
                                First name: <input type='text' name='firstName'/>
                                <br/>
                                Last name: <input type='text' name='lastName'/>
                                <br/>
                                <input type='button' value='Save' onclick='sendFormData();'/>
                            </form>
                            </body>
                            </html>");
        BrowserView browserView = new WinFormsBrowserView(browser);
        this.Controls.Add((Control)browserView.GetComponent());
    }
    public class Account
    {
        private Form Form;
        public Account(Form form)
        {
            this.Form = form;
        }
        public void Save(String firstName, String lastName)
        {
            string _firstname = firstName;
            Form.Invoke(new Action(() =>
            {
                Form.Hide();
            }));
            SecondForm frm = new SecondForm(firstName);
            frm.ShowDialog(); 
        }
    }
}