无法从Awesomium.Net + XNA中的localhost加载html

时间:2015-06-22 16:42:35

标签: c# webview xna awesomium

我正在XNA中编写一个使用Awesomium webviews作为UI的游戏。使用此class.

将webview呈现给Texture2D

使用本地html文件很好用,但我还需要使用一些XML而且Awesomium似乎不允许这样做(将FileAccessFromFileURL和WebSecurity设置为false什么都不做)。我的解决方案是在本地Web服务器上托管我的HTML / XML UI,将class用于服务器。

网络服务器运行正常并可从我的浏览器访问,但当我将我的网页浏览源设置为http://localhost:8023/myindex.html时,我会看到空白窗口。当我使用Awesomium WPF控件访问localhost时,我没有得到这个输出,当我远程托管html时也没有得到它。这让我觉得问题是由我的班级处理URI的原因造成的。

    public struct AwesomiumMenu
{
    public WebView webView;
    public Microsoft.Xna.Framework.Rectangle Rectangle;

    public AwesomiumMenu(string Source, Microsoft.Xna.Framework.Rectangle rectangle)
    {
        // WebPreferences to disable same-origin policy so we can load local XML + remove scrollbars
        const string SCROLLBAR_CSS = "::-webkit-scrollbar { visibility: hidden; }";
        WebSession session = WebCore.CreateWebSession(new WebPreferences()
        {
            CustomCSS = SCROLLBAR_CSS,
            UniversalAccessFromFileURL = true,
            FileAccessFromFileURL = true,
            WebSecurity = false
        });

        //// CSS styling
        //const string SCROLLBAR_CSS = "::-webkit-scrollbar { visibility: hidden; }";
        //WebCore.Initialize(new WebConfig()
        //{
        //    CustomCSS = SCROLLBAR_CSS
        //});

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

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

2 个答案:

答案 0 :(得分:0)

我使用mongoose作为本地服务器 - 非常易于使用。我做了最简单的测试应用程序,它对我有用。这是整个代码:

public partial class Form1 : Form
{
    private Awesomium.Windows.Forms.WebControl webControl1;

    public Form1()
    {
        if (!WebCore.IsInitialized)
        {
            WebCore.Initialize(new WebConfig(), true);
        }

        InitializeComponent();

        this.webControl1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.webControl1.Location = new System.Drawing.Point(0, 0);
        this.webControl1.Size = new System.Drawing.Size(1134, 681);
        this.webControl1.TabIndex = 0;

        this.webControl1.WebSession = WebCore.CreateWebSession("%APPDATA%\\Test", new WebPreferences
        {
            CustomCSS = "body { overflow:hidden; }",
            WebSecurity = false,
            DefaultEncoding = "UTF-8",
        });

        this.webControl1.Source = new Uri("http://localhost:8080/Test/test.html");
    }
}

答案 1 :(得分:0)

由于您正在进行XNA游戏,为什么不将您的界面作为资源嵌入DLL文件中?这就是我为我的游戏所做的一切,它运作良好

在我的游戏中,我是​​从DLL加载的界面,并且根据设计,我会阻止我的上下文之外的网址导航(没有外部网址导航)。

为了实现这一点,我实现了一个DataSource类,在OnRequest调用中,从dll加载资源,这里是(有点优化并从我的项目中直接提取,但你应该理解它)< / p>

class SampleDataSource : DataSource {
     Assembly InterfaceAssembly;
     string[] names;

     public SampleDataSource() {
        InterfaceAssembly = typeof(Interface.HTMLInterface).Assembly;

        // Get the names of all the resources in the assembly
        names = InterfaceAssembly.GetManifestResourceNames();
        Array.Sort(names, CaseInsensitiveComparer.Default);
     }

     protected override void OnRequest(DataSourceRequest request) {
        var response = new DataSourceResponse();

        string[] parts = request.Path.Replace("\\", "/").Split('/');

        for (int i = 0; i < parts.Length - 1; i++) {
           parts[i] = parts[i].Replace(".", "_").Replace("-", "_");
        }

        string resourcePath = "Interface.html." + request.Path
           //.Replace(".", "_")
           .Replace("/", ".")
           .Replace("\\", ".");

        resourcePath = "Interface.html." + String.Join(".", parts);


        // Find the image in the names array
        int pos = Array.BinarySearch(names, resourcePath, CaseInsensitiveComparer.Default);

        if (pos > -1) {
           resourcePath = names[pos];
        }

        byte[] data = null;
        using (Stream stream = InterfaceAssembly.GetManifestResourceStream(resourcePath)) {
           data = new byte[stream.Length];
           stream.Read(data, 0, data.Length);
        }

        IntPtr unmanagedPointer = IntPtr.Zero;
        try {
           unmanagedPointer = Marshal.AllocHGlobal(data.Length);
           Marshal.Copy(data, 0, unmanagedPointer, data.Length);

           response.Buffer = unmanagedPointer;

           switch (System.IO.Path.GetExtension(request.Path).ToLower()) {
              case ".html":
              case ".htm":
                 response.MimeType = "text/html";
                 break;
              case ".js":
                 response.MimeType = "application/javascript";
                 break;
              case ".css":
                 response.MimeType = "text/css";
                 break;
              case ".png":
                 response.MimeType = "image/png";
                 break;
              case ".jpeg":
              case ".jpg":
                 response.MimeType = "image/jpeg";
                 break;
              default:
                 response.MimeType = "text/html";
                 break;
           }

           response.Size = (uint)data.Length;
           SendResponse(request, response);
        }
        catch (Exception e) {
           throw e;
        }
        finally {
           Marshal.FreeHGlobal(unmanagedPointer);
        }
     }
  }

您可以在官方文档docs.awesomium.net上找到有关DataSource的信息

无论如何这就是我将数据源链接到我的webview的方式:

WebView.WebSession.AddDataSource("sample", new SampleDataSource());