I\'m trying to render render a browser in side my monogame project, for drawing some interface & stuff. I\'ve done this in the past with older versions of awesomium with no
I have just got this working, you have to create a new thread, then call Run, then listen for an event to be raised by WebCore which will have created a new SynchronizationContext by then. You then want to keep a reference to that context on your main thread...
Thread awesomiumThread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
{
WebCore.Started += (s, e) => {
awesomiumContext = SynchronizationContext.Current;
};
WebCore.Run();
}));
awesomiumThread.Start();
WebCore.Initialize(new WebConfig() { });
... you can then call all your WebView methods using that SynchronizationContext ...
awesomiumContext.Post(state =>
{
this.WebView.Source = "http://www.google.com";
}, null);
I will tidy this up with a future edit, but to get you guys started, here is my component...
using Awesomium.Core;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AwesomiumComponent
{
public class BasicAwesomiumComponent : DrawableGameComponent
{
private Byte[] imageBytes;
private Rectangle area;
private Rectangle? newArea;
private Boolean resizing;
private SpriteBatch spriteBatch;
private Texture2D WebViewTexture { get; set; }
private SynchronizationContext awesomiumContext = null;
private WebView WebView { get; set; }
private BitmapSurface Surface { get; set; }
private MouseState lastMouseState;
private MouseState currentMouseState;
private Keys[] lastPressedKeys;
private Keys[] currentPressedKeys = new Keys[0];
private static ManualResetEvent awesomiumReady = new ManualResetEvent(false);
public Rectangle Area
{
get { return this.area; }
set
{
this.newArea = value;
}
}
public BasicAwesomiumComponent(Game game, Rectangle area)
: base(game)
{
this.area = area;
this.spriteBatch = new SpriteBatch(game.GraphicsDevice);
Thread awesomiumThread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
{
WebCore.Started += (s, e) => {
awesomiumContext = SynchronizationContext.Current;
awesomiumReady.Set();
};
WebCore.Run();
}));
awesomiumThread.Start();
WebCore.Initialize(new WebConfig() { });
awesomiumReady.WaitOne();
awesomiumContext.Post(state =>
{
this.WebView = WebCore.CreateWebView(this.area.Width, this.area.Height, WebViewType.Offscreen);
this.WebView.IsTransparent = true;
this.WebView.CreateSurface += (s, e) =>
{
this.Surface = new BitmapSurface(this.area.Width, this.area.Height);
e.Surface = this.Surface;
};
}, null);
}
public void SetResourceInterceptor(IResourceInterceptor interceptor)
{
awesomiumContext.Post(state =>
{
WebCore.ResourceInterceptor = interceptor;
}, null);
}
public void Execute(string method, params object[] args)
{
string script = string.Format("viewModel.{0}({1})", method, string.Join(",", args.Select(x => "\"" + x.ToString() + "\"")));
this.WebView.ExecuteJavascript(script);
}
public void RegisterFunction(string methodName, Action