在XNA项目中打开webBrowser?

时间:2011-01-13 20:03:35

标签: c# xna browser

我有XNA游戏,打开一个包含webBrowser的窗口表单。

每次打开表单时,都会显示以下错误:

    System.Threading.ThreadStateException was unhandled
  Message=ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
       at System.Windows.Forms.WebBrowser..ctor()
       at GameOfLifeV2._0.Help.InitializeComponent() in C:\Users\Gilad\Documents\C# projects\GameOfLifeV2.0\GameOfLifeV2.0\GameOfLifeV2.0\Help.Designer.cs:line 33
       at GameOfLifeV2._0.Help..ctor() in C:\Users\Gilad\Documents\C# projects\GameOfLifeV2.0\GameOfLifeV2.0\GameOfLifeV2.0\Help.cs:line 17
       at GameOfLifeV2._0.Game.RespondKeyboard(KeyboardState state) in C:\Users\Gilad\Documents\C# projects\GameOfLifeV2.0\GameOfLifeV2.0\GameOfLifeV2.0\Game.cs:line 111
       at GameOfLifeV2._0.Game.Update(GameTime gameTime) in C:\Users\Gilad\Documents\C# projects\GameOfLifeV2.0\GameOfLifeV2.0\GameOfLifeV2.0\Game.cs:line 68
       at Microsoft.Xna.Framework.Game.Tick()
       at Microsoft.Xna.Framework.Game.HostIdle(Object sender, EventArgs e)
       at Microsoft.Xna.Framework.GameHost.OnIdle()
       at Microsoft.Xna.Framework.WindowsGameHost.RunOneFrame()
       at Microsoft.Xna.Framework.WindowsGameHost.ApplicationIdle(Object sender, EventArgs e)
       at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Microsoft.Xna.Framework.WindowsGameHost.Run()
       at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
       at Microsoft.Xna.Framework.Game.Run()
       at GameOfLifeV2._0.Program.Main(String[] args) in C:\Users\Gilad\Documents\C# projects\GameOfLifeV2.0\GameOfLifeV2.0\GameOfLifeV2.0\Program.cs:line 15
  InnerException:

在以下行中:

this.InfoBrowsersd = new System.Windows.Forms.WebBrowser();

欢迎提供解决方案:)

替代问题: 有没有更好的方法来显示帮助文档,然后创建HTML文件并在webBrowser控件中显示它们?

3 个答案:

答案 0 :(得分:2)

您是否需要能够在Xbox上显示帮助,或仅在Windows上显示帮助?要使其在Windows上运行,只需在Program.cs中为[STAThread]函数添加Main()属性:

[STAThread]
static void Main(string[] args)
{
    ...
}

有关STA与MTA模型的背景信息,请参阅http://blogs.msdn.com/b/oldnewthing/archive/2008/04/24/8420242.aspx

答案 1 :(得分:1)

您是否尝试过通过

启动帮助文件
System.Diagnostics.Process.Start("c:\...\MyHelpFile.html")  

这将在用户默认设置的任何程序中打开您的HTML文件,以查看HTML文件。 (您必须将上述代码中的路径替换为实际HTML文件的路径)。

既然你说你必须在游戏中显示它,那么我认为你唯一的另一个选择是尝试在它自己的主题中打开表单。

  Thread helpThread = new Thread(new ThreadStart(delegate() { 
     this.InfoBrowsersd = new System.Windows.Forms.WebBrowser();
  })); 

  helpThread.Start(); 

希望我的语法正确,但试一试,看看是否能给你想要的结果。我认为你遇到了一个问题,因为你无法从游戏循环线程中启动WinForm。它并不是真的旨在支持它。因此,在新线程中启动它可能会帮助您解决它。

答案 2 :(得分:1)

根据目标平台的不同,您可以使用Awesomium.net wrapper在游戏中呈现HTML文件。