无法在WebBrowser控件中使用Console.Log() [英] Unable to use Console.Log() within a WebBrowser Control

查看:69
本文介绍了无法在WebBrowser控件中使用Console.Log()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用C#Windows Forms编写Javascript编码UI.这是我按下运行"按钮时的代码,以防万一:

So I'm writing a Javascript coding UI using C# Windows Forms. This is my code for when the "Run" button is pressed, in case it helps:

//If the button in the upper-right corner is clicked
    private void run_Click(object sender, EventArgs e)
    {
        //If the program isn't running
        if (!running)
        {
            //Show the web browser
            webBrowser1.Visible = true;
            richTextBox1.Visible = false;
            //Set the label to Running
            status.Text = "Running";
            //Set the html text to this below
            webBrowser1.DocumentText = "<!DOCTYPE html><html><body><script>\n" + richTextBox1.Text + "\n</script></body></html>";
            //Set the "run" button to "stop"
            run.Text = "Stop";
            //Set the status to running
            running = true;
        }
        //otherwise
        else
        {
            //Show the text box
            webBrowser1.Visible = false;
            richTextBox1.Visible = true;
            //Set the label to Ready
            status.Text = "Ready";
            //Go to nothingness
            webBrowser1.Navigate("about:blank");
            //Set the "stop" button to "run"
            run.Text = "Run";
            //Set the status to not running
            running = false;
        }
    }

我运行程序,并且大部分情况下一切正常.但是,当我尝试使用console.log()命令时,出现以下错误:

I run the program, and for the most part, everything works fine. However, when I try to use the console.log() command, the following error appears:

'console' is undefined

我还尝试使用Console.Log(我实际上不懂Javascript;只是尽力而为),但是返回的错误相同,即"Console"未定义.

I also try Console.Log (I actually don't know Javascript; just trying my best) but that returns the same error, that 'Console' is undefined.

此外,一旦使console.log正常工作,如何在WebBrowser控件上打开控制台?我已经尝试过搜索互联网,但是这些问题都没有解决.

Also, once I get console.log working, how do I open the console on the WebBrowser control? I've tried searching the internet, but nothing has come up on either of these questions.

推荐答案

是的,控制台类在您的浏览器控件中不可用,但是您可以创建这样的记录器类

Yes, Console class is not available in your browser control, but you can create a logger class like this

[ComVisible(true)]
public class Logger
{
    public void log(string s)
    {
        Console.WriteLine(s);
    }
}

并在浏览器控件中使用它

and use it in your browser control

webBrowser1.ObjectForScripting = new Logger();
webBrowser1.DocumentText = "<script>external.log('TEST');</script>";

这篇关于无法在WebBrowser控件中使用Console.Log()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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