Awesomium 1.7.5.1,WinForms,WebControl obj.ExecuteJavascript

时间:2015-06-07 09:03:49

标签: winforms awesomium web-control

我想将html代码插入现有的html代码中。 但我没有看到结果。这是代码C#:

1)Program.cs

public partial class Form1 : Form
{
    public Form1()
    {            
        InitializeComponent();
        wUI.DocumentReady += wUI_DocumentReady;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // code here ?           
    }

    void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
    {
        wUI.LoadHTML("<html><body>sadasdsad</body></html>");
        HtmlManager html = HtmlManager.Instance;
        string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
        html.Add("<a href=\"{0}\" class=\"{1}\" id=\"{2}\">{3}</a>", placeholders);
        html.InnerCode(html.Code, wUI, "body");
        wUI.Refresh();
    }
}

2)HtmlManager.cs

public sealed class HtmlManager
{
    private static readonly Lazy<HtmlManager> InstanceField = new Lazy<HtmlManager>(() => new HtmlManager());
    private StringBuilder _stringBuilder = null;
    public string Code { get { return _stringBuilder.ToString(); } }  

    private HtmlManager() 
    {
        if (_stringBuilder != null)
            _stringBuilder.Clear();
        _stringBuilder = new StringBuilder();
    }

    public static HtmlManager Instance { get { return InstanceField.Value; } }

    public void Add(string row, string[] placeholders = null)
    {
        if (placeholders != null)
            _stringBuilder.AppendLine(string.Format(row, placeholders));
        _stringBuilder.AppendLine(row);
    }

    public void InnerCode(string code, object sender, string afterTag = "html")
    {
        Awesomium.Windows.Forms.WebControl ui = (Awesomium.Windows.Forms.WebControl)sender;
        ui.ExecuteJavascript(string.Format("document.getElementsByTagName({0})[0].innerHTML({1})", afterTag, code));            
    }

    public void Clear()
    {
        _stringBuilder.Clear();
    }
} 

事件(DocumentReady)没有发生,我不相信,也许我错了?

UP:我尝试这样做:

private void Form1_Load(object sender, EventArgs e)
    {
        wUI.LoadHTML("<html><body>sadasdsad</body></html>");       
    }

    void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
    {

        HtmlManager html = HtmlManager.Instance;
        string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
        html.Add("<a href=\"{0}\" class=\"{1}\" id=\"{2}\">{3}</a>", placeholders);

        wUI.ExecuteJavascript("document.getElementsByTagName('body').innerHTML(\"sometext\")");  

        //html.InnerCode(html.Code, wUI, "body");
        //wUI.Refresh();
    }

没有结果

UP 2:

public void Add(string row, string[] placeholders = null)
    {
        if (placeholders != null)
            _stringBuilder.AppendLine(string.Format(row, placeholders));
        if (placeholders == null)
            _stringBuilder.AppendLine(row);
    }

UP 3:

使用:

wUI.Source = new Uri(@"http://google.com");
Form1_Load中的

1 个答案:

答案 0 :(得分:0)

您可以使用LoadHtml方法,但仅在文档完全加载后(不要与DocumentReadyState.Ready混淆)它至少对我有用:

    private void WebControl_DocumentReady(object sender, DocumentReadyEventArgs e)
    {
        if (e.ReadyState != DocumentReadyState.Loaded) return;


    }

但是作为初始化,您应该使用Source属性,就像您在第三次更新中所写的那样