在XNA中用Awesomium检测onClick

时间:2015-04-13 15:12:45

标签: c# xna awesomium

我正在使用Awesomium为XNA框架游戏构建菜单系统。菜单是可见的,mouseState事件正在成功注入我的webViews(有一些CSS翻转工作)。但是当我在菜单按钮中触发onClick事件时,我似乎无法在我的C#代码中执行方法。

C#

        private void OnViewProcessCreated(object sender, EventArgs e)
    {
        JSObject menu = webView.CreateGlobalJavascriptObject("menu");

        if (menu == null)
            return;

        using (menu)
            menu.BindAsync("onButtonClick", myJSMethodHandler);
    }

    private void myJSMethodHandler(object sender, JavascriptMethodEventArgs e)
    {
        if (e.MethodName == "onButtonClick")
        {
            WebCore.Shutdown();
        }
    }

JS / HTML

<!DOCTYPE html>
<html>
<head>
  <title>Main UI</title>
  <link rel="stylesheet" type="text/css" href="style.css">
    <script>
    {
        object.onclick = menu.onButtonClick;
    }
    </script>

</head>

<body>
    <div class="d1">
        <p><h1 class="button1" id="b1" onclick="menu.onButtonClick();">Agents</h1></p>
        <p><h1 class="button1" id="b2" onclick="">Research</h1></p>
        <p><h1 class="button1" id="b3" onclick="">Infrastructure</h1></p>
        <p><h1 class="button1" id="b4" onclick="">Financial</h1></p>
    </div>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

删除不必要的JS函数并直接从元素调用绑定函数。

    <!DOCTYPE html>
<html>
<head>
  <title>Main UI</title>
  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>
    <div class="d1">
        <p><h1 class="button1" id="b1" onclick="menu.onButtonClick();">Agents</h1></p>
        <p><h1 class="button1" id="b2" onclick="">Research</h1></p>
        <p><h1 class="button1" id="b3" onclick="">Infrastructure</h1></p>
        <p><h1 class="button1" id="b4" onclick="">Financial</h1></p>
    </div>
</body>

</html>

还意识到OnViewProcessCreated函数永远不会被调用,不知道为什么。在主要的Awesomium函数中创建了一个JSObject,运行正常。

    public AwesomiumMenu(string Source, Microsoft.Xna.Framework.Rectangle rectangle)
    {
        // CSS styling
        const string SCROLLBAR_CSS = "::-webkit-scrollbar { visibility: hidden; }";
        WebCore.Initialize(new WebConfig()
        {
            CustomCSS = SCROLLBAR_CSS
        });

        webView = WebCore.CreateWebView(rectangle.Width, rectangle.Height);
        webView.ReduceMemoryUsage();
        webView.Source = Source.ToUri();
        webView.IsTransparent = true;

        while (webView.IsLoading)
            WebCore.Update();

        Rectangle = rectangle;
        JSObject menu = webView.CreateGlobalJavascriptObject("menu");
        menu.BindAsync("onButtonClick", myJSMethodHandler);
    }