紧凑框架中的网络浏览器

Webbrowser in compact framework

我想在 .NET Compact Framework 3.5 项目中使用 WebBrowser 组件。我遇到了关于此的矛盾信息。

如果我只是尝试使用它,我会遇到以下异常:

System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
at System.Windows.Forms.WebBrowser..ctor()
at dce.pd.dialogmgr.baseimpl.DialogDeviceImpl.createGui(XmlElement pDialogManagerNode) in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\baseimpl\DialogDeviceImpl.cs:line 362
at dce.pd.dialogmgr.baseimpl.DialogDeviceImpl.init() in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\baseimpl\DialogDeviceImpl.cs:line 118
at dce.pd.dialogmgr.thinimpl.ThinClient.createDeviceImpl() in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\thinimpl\ThinClient.cs:line 82
at dce.pd.dialogmgr.thinimpl.ThinClient.init() in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\thinimpl\ThinClient.cs:line 87
at dce.pd.dialogmgr.thinimpl.ThinClient.form_Load(Object sender, EventArgs e) in C:\sourceview\CS.NETCF.2008\dce\pd\dialogmgr\thinimpl\ThinClient.cs:line 66
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

对我来说,这听起来像是我必须将 [STAThread] 指令放入我的 Main 方法。

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

但这也不起作用,因为 the [STAThread] option is not available in compact framework.。因此,应用程序现在甚至无法编译。

然而,根据 Microsoft's documentationWebBrowser 组件可用于紧凑型框架项目。甚至还有一些例子。 (不幸的是,没有提供完整的代码)。

那么,我错过了什么?

终于找到问题所在了

实际上,上面的异常(即 System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.)仅在我尝试直接在笔记本电脑上 运行 编译后的可执行文件时出现。

模拟器不适合我。我现在终于让模拟器工作了。我意识到我必须 select 一个 "emulator" 设备,那种名称以 "emulator" 结尾的设备。 (例如 "Windows Mobile 6 Emulator")。您需要在测试前连接这些设备,使用工具栏中的 "connect to device" 按钮。建立连接后,应用程序实际上 运行在模拟器上运行正常。

问题仍然存在:为什么应用程序 运行 不直接在我的笔记本电脑上运行,而在模拟器中 运行 运行良好.但这是一个不同的问题。

当然操作系统不同。但我的印象是,系统使用不同版本的 .NET 框架。以前 我错误地认为所有紧凑型框架应用程序也可以 运行 在配备完善的 .net 框架的笔记本电脑上。 现在看来这是不正确的。