2014-04-12 5 views
8

메신저 프로젝트에서 브라우저 렌더링을 시도 중입니다. 일부 인터페이스를 그리는 데 & 물건을 그리는 중입니다. 나는 이전 버전의 awesomium으로 문제없이이 작업을 해왔습니다. 그러나 나는이 새로운 버전에서 어떻게 awesomium을 초기화해야하는지 알아낼 수 없다. 내가 어떻게하려고하는지에 관계없이 오류가 발생한다. WebCore.Update() 대신 WebCore.Run()을 한 번 호출하면됩니다. 그러나 해당 메서드에서 varius 예외가 발생합니다.Monogame 프로젝트에 Awesomium 1.7.4.2를 어떻게 구현합니까?

  1. 여기

내 시도 중 일부는 내 프로젝트에 Awesomium 1.7.4.2

  • Refrenced \1.7.4.2\wrappers\Awesomium.NET\Assemblies\Packed\Awesomium.Core.dll를 설치합니다 :

    을 여기

    는 내가 지금까지 수행 한 단계입니다
    WebCore.Initialize(new WebConfig()); 
        WebCore.Run(); 
        //Error: Starting an update loop on a thread with an existing message loop, is not supported. 
    


    1,236,583,


    WebCore.Initialize(new WebConfig()); 
        WebView WebView = WebCore.CreateWebView(500, 400); 
        WebView.Source = new Uri("http://www.google.com"); 
        WebView.DocumentReady += (sender, e) => 
        { 
         JSObject js = WebView.CreateGlobalJavascriptObject("w"); 
        }; 
        // No errors, but DocumentReady is never fired.. 
    
    는 또한 NullRefrence 오류를 얻을 관리해야하고, 내가 WebCore.Run()를 호출하기 전에에 Thread.sleep (400)를 기다리면, 그것은 단지 WebCore.Run를 입력()와 그 라인을 완료하지 않습니다.

    어떻게 설정하나요? 어디에서나 예제를 찾을 수 없습니다. 온라인 예제는 여전히 사용법을 알려줍니다.

  • +0

    나는 똑같은 고민하고 있어요. –

    +0

    그래, 나는 그것을 해결하지 못했다 ... 나는 1.7.3.0 버전으로 다운 그레이드했다. 그 버전은 잘 작동한다. 만약 당신이 그걸 가지고 견습생이 있다면 기꺼이 도와 드리겠습니다. – BjarkeCK

    답변

    13

    방금이 작업을 했으므로 새 스레드를 만든 다음 실행을 호출하고 생성 된 WebCore에 의해 발생되는 이벤트를 수신해야합니다. 그때까지 새로운 SynchronizationContext. 그런 다음

    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() { }); 
    

    ... 당신은 그 사용하여 모든 웹보기의 메서드를 호출 할 수 있습니다 ... 당신의 주 스레드에서 해당 컨텍스트에 대한 참조를 유지하려는 SynchronizationContext에 ...

    awesomiumContext.Post(state => 
    { 
        this.WebView.Source = "http://www.google.com"; 
    }, null); 
    

    나는 것 미래의 편집과 함께이 깔끔하지만, 너희들 시작하기 위해 여기에 내 구성 요소입니다 ...

    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<object, CancelEventArgs> handler) 
         { 
          // Create and acquire a Global Javascript object. 
          // These object persist for the lifetime of the web-view. 
          using (JSObject myGlobalObject = this.WebView.CreateGlobalJavascriptObject("game")) 
          { 
           // The handler is of type JavascriptMethodEventHandler. Here we define it 
           // using a lambda expression. 
    
           myGlobalObject.Bind(methodName, true, (s, e) => 
           { 
            handler(s, e); 
            // Provide a response. 
            e.Result = "My response"; 
           }); 
          } 
         } 
    
         public void Load() 
         { 
          LoadContent(); 
         } 
    
         protected override void LoadContent() 
         { 
          if (this.area.IsEmpty) 
          { 
           this.area = this.GraphicsDevice.Viewport.Bounds; 
           this.newArea = this.GraphicsDevice.Viewport.Bounds; 
          } 
          this.WebViewTexture = new Texture2D(this.Game.GraphicsDevice, this.area.Width, this.area.Height, false, SurfaceFormat.Color); 
    
          this.imageBytes = new Byte[this.area.Width * 4 * this.area.Height]; 
         } 
    
         public override void Update(GameTime gameTime) 
         { 
          awesomiumContext.Post(state => 
          { 
           if (this.newArea.HasValue && !this.resizing && gameTime.TotalGameTime.TotalSeconds > 0.10f) 
           { 
            this.area = this.newArea.Value; 
            if (this.area.IsEmpty) 
             this.area = this.GraphicsDevice.Viewport.Bounds; 
    
            this.WebView.Resize(this.area.Width, this.area.Height); 
            this.WebViewTexture = new Texture2D(this.Game.GraphicsDevice, this.area.Width, this.area.Height, false, SurfaceFormat.Color); 
            this.imageBytes = new Byte[this.area.Width * 4 * this.area.Height]; 
            this.resizing = true; 
    
            this.newArea = null; 
           } 
    
           lastMouseState = currentMouseState; 
           currentMouseState = Mouse.GetState(); 
    
           this.WebView.InjectMouseMove(currentMouseState.X - this.area.X, currentMouseState.Y - this.area.Y); 
    
           if (currentMouseState.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released) 
           { 
            this.WebView.InjectMouseDown(MouseButton.Left); 
           } 
           if (currentMouseState.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed) 
           { 
            this.WebView.InjectMouseUp(MouseButton.Left); 
           } 
           if (currentMouseState.RightButton == ButtonState.Pressed && lastMouseState.RightButton == ButtonState.Released) 
           { 
            this.WebView.InjectMouseDown(MouseButton.Right); 
           } 
           if (currentMouseState.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed) 
           { 
            this.WebView.InjectMouseUp(MouseButton.Right); 
           } 
           if (currentMouseState.MiddleButton == ButtonState.Pressed && lastMouseState.MiddleButton == ButtonState.Released) 
           { 
            this.WebView.InjectMouseDown(MouseButton.Middle); 
           } 
           if (currentMouseState.MiddleButton == ButtonState.Released && lastMouseState.MiddleButton == ButtonState.Pressed) 
           { 
            this.WebView.InjectMouseUp(MouseButton.Middle); 
           } 
    
           if (currentMouseState.ScrollWheelValue != lastMouseState.ScrollWheelValue) 
           { 
            this.WebView.InjectMouseWheel((currentMouseState.ScrollWheelValue - lastMouseState.ScrollWheelValue), 0); 
           } 
    
           lastPressedKeys = currentPressedKeys; 
           currentPressedKeys = Keyboard.GetState().GetPressedKeys(); 
    
           // Key Down 
           foreach (var key in currentPressedKeys) 
           { 
            if (!lastPressedKeys.Contains(key)) 
            { 
             this.WebView.InjectKeyboardEvent(new WebKeyboardEvent() 
             { 
              Type = WebKeyboardEventType.KeyDown, 
              VirtualKeyCode = (VirtualKey)(int)key, 
              NativeKeyCode = (int)key 
             }); 
    
             if ((int)key >= 65 && (int)key <= 90) 
             { 
              this.WebView.InjectKeyboardEvent(new WebKeyboardEvent() 
              { 
               Type = WebKeyboardEventType.Char, 
               Text = key.ToString().ToLower() 
              }); 
             } 
             else if (key == Keys.Space) 
             { 
              this.WebView.InjectKeyboardEvent(new WebKeyboardEvent() 
              { 
               Type = WebKeyboardEventType.Char, 
               Text = " " 
              }); 
             } 
            } 
           } 
    
           // Key Up 
           foreach (var key in lastPressedKeys) 
           { 
            if (!currentPressedKeys.Contains(key)) 
            { 
             this.WebView.InjectKeyboardEvent(new WebKeyboardEvent() 
             { 
              Type = WebKeyboardEventType.KeyUp, 
              VirtualKeyCode = (VirtualKey)(int)key, 
              NativeKeyCode = (int)key 
             }); 
            } 
           } 
    
          }, null); 
    
          base.Update(gameTime); 
         } 
    
         public override void Draw(GameTime gameTime) 
         { 
          awesomiumContext.Post(state => 
          { 
           if (Surface != null && Surface.IsDirty && !resizing) 
           { 
            unsafe 
            { 
             // This part saves us from double copying everything. 
             fixed (Byte* imagePtr = this.imageBytes) 
             { 
              Surface.CopyTo((IntPtr)imagePtr, Surface.Width * 4, 4, true, false); 
             } 
            } 
            this.WebViewTexture.SetData(this.imageBytes); 
           } 
          }, null); 
    
          Vector2 pos = new Vector2(0, 0); 
          spriteBatch.Begin(); 
          spriteBatch.Draw(this.WebViewTexture, pos, Color.White); 
          spriteBatch.End(); 
          GraphicsDevice.Textures[0] = null; 
    
          base.Draw(gameTime); 
         } 
    
         public Uri Source 
         { 
          get 
          { 
           return this.WebView.Source; 
          } 
          set 
          { 
           awesomiumContext.Post(state => 
           { 
            this.WebView.Source = value; 
           }, null); 
          } 
         } 
    
         public void Resize(int width, int height) 
         { 
          this.newArea = new Rectangle(0, 0, width, height); ; 
         } 
        } 
    } 
    
    +0

    굉장! 나는 결코 그것을 이해하지 못했을 것이다. 그 정보를 어디서 발견했는지 물어봐도 될까요? – BjarkeCK

    +0

    나는 Run 메서드에 대한 주석을 읽고 작동시킬 때까지 해킹했다. 설명을 보려면 Run 메서드의 GoToDefinition을 클릭 한 다음 축소 된 주석을 확장합니다. –

    +0

    위대한 작품 딘. 나는 1000 표를 줄 수 있었으면 좋겠다. – ZafarYousafi