C# XNA 4.0中的WebBrowser未导航

C# XNA 4.0中的WebBrowser未导航,c#,.net,xna,webbrowser-control,textures,C#,.net,Xna,Webbrowser Control,Textures,我一直在尝试让WebBrowser在XNA4.0中绘制纹理,我找到了一些关于如何绘制纹理的指南。问题是当我尝试实现它时,无论是更改Url属性还是调用Navigate()它都不会加载页面。我觉得我对所需的线程有点无知,因为我的项目不是以STA线程启动的,所以我创建了一个单独的线程来启动web浏览器并渲染为位图 我是这样开始的: public void LoadTexture(GraphicsDevice gfx, ContentManager content, string filename, f

我一直在尝试让WebBrowser在XNA4.0中绘制纹理,我找到了一些关于如何绘制纹理的指南。问题是当我尝试实现它时,无论是更改
Url
属性还是调用
Navigate()
它都不会加载页面。我觉得我对所需的线程有点无知,因为我的项目不是以STA线程启动的,所以我创建了一个单独的线程来启动web浏览器并渲染为位图

我是这样开始的:

public void LoadTexture(GraphicsDevice gfx, ContentManager content, string filename, float duration = -1f)
{
    this.gfx = gfx;
    this.filename = filename;
    this.duration = duration;

    _resetEvent = new AutoResetEvent(false);
    Thread thread = new Thread(GetWebScreenshot);
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    _resetEvent.WaitOne();
}
下面是
GetWebScreenshot

public void GetWebScreenshot()
{
    this.web = new WebBrowser();
    this.web.Size = new Size(gfx.Viewport.Width, gfx.Viewport.Height);
    this.web.Url = new Uri(this.filename);

    while (this.web.ReadyState != WebBrowserReadyState.Complete)
    {
        if (this.web.ReadyState != WebBrowserReadyState.Uninitialized)
        {
            Console.WriteLine(this.web.ReadyState.ToString());
        }
    }

    bitmap = new Bitmap(this.gfx.Viewport.Width, this.gfx.Viewport.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    this.web.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height));
    this.texture = BitmapToTexture2D(this.gfx, bitmap);
    _resetEvent.Set();
}

ReadyState
属性从未从
Uninitialized
更改,我也尝试过使用
DocumentReady
事件,但从未触发。我也尝试了
Join()
而不是
AutoResetEvent
,但似乎没有任何效果。

我是对的,这是我的无知。ActiveX控件和单线程单元的关键之处在于需要对消息队列进行压缩。因此,现在我已将代码重组为以下内容:

public void LoadTexture(GraphicsDevice gfx, ContentManager content, string filename, float duration = -1f)
{
    this.gfx = gfx;
    this.filename = filename;
    this.duration = duration;

    Thread thread = new Thread(GetWebScreenshot);
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

public void GetWebScreenshot()
{
    this.web = new WebBrowser();
    this.web.Size = new Size(gfx.Viewport.Width, gfx.Viewport.Height);
    this.web.Url = new Uri(this.filename);
    this.web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted);
    Application.Run(); // Starts pumping the message queue (and keeps the thread running)
}

void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    Bitmap bitmap = new Bitmap(this.gfx.Viewport.Width, this.gfx.Viewport.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    this.web.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height));
    this.texture = HTMLTextureFactoryMachine.BitmapToTexture2D(this.gfx, bitmap);
    Application.ExitThread(); // Exits the thread
}
这没问题