.NET开发WinCE程序之使用软键盘(System.WindowsCE.Forms命名空间)兼容WinCE和桌面操作系统之解决方案

开发环境:VS2008

使用系统:Wince 5.0

项目版本:.Net Framework 2.0

编译版本:.Net Framework 3.5

程序要求:能够在WinXP和WinCE下同时使用

  最近在开发WinCE系统下的应用程序:其中用到了Inputpanel类,实现用户点击文本框后自动显示输入面板,方便用户的输入。

这个类需要引用System.WindowsCE.Forms才能正常使用。

程序代码:

代码
using Microsoft.WindowsCE.Forms;
public class Common
{
/// <summary>
/// 软键盘
/// </summary>
public static InputPanel _softKeyBoard = new InputPanel();
/// <summary>
/// 显示/隐藏 软键盘
/// </summary>
public static void ShowHideSoftKeyBoard(Boolean isShow)
{
_softKeyBoard.Enabled
= isShow;
}
}

完成了这个功能编译后发现程序在WinCE下能够正常使用,但是在WinXP不能正常使用了,产生以下错误代码:

有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。

************** 异常文本 **************
System.TypeInitializationException: “SmartDeviceProject1.Common”的类型初始值设定项引发异常。 ---> System.IO.FileNotFoundException: 未能加载文件或程序集“Microsoft.WindowsCE.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac”或它的某一个依赖项。系统找不到指定的文件。
文件名:“Microsoft.WindowsCE.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac”
   在 SmartDeviceProject1.Common..cctor()

警告: 程序集绑定日志记录被关闭。
要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1。
注意: 会有一些与程序集绑定失败日志记录关联的性能损失。
要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。

   --- 内部异常堆栈跟踪的结尾 ---
   在 SmartDeviceProject1.Common.get_CurrentAppPath()
   在 SmartDeviceProject1.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\北京华运电利载波技术有限公司\小口径摄像式直抄系统\MIT系统\Source\StartDA\SmartDeviceProject1\Form1.cs:行号 23
   在 System.Windows.Forms.Control.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
   在 System.Windows.Forms.Button.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** 已加载的程序集 **************
mscorlib
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3615 (GDR.050727-3600)
    基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
SmartDeviceProject1
    程序集版本: 1.0.0.0
    Win32 版本: 1.0.0.0
    基本代码: file:///E:/北京华运电利载波技术有限公司/小口径摄像式直抄系统/MIT系统/Source/StartDA/SmartDeviceProject1/bin/Debug/SmartDeviceProject1.exe
----------------------------------------
System.Windows.Forms
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3614 (GDR.050727-3600)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
mscorlib.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3615 (GDR.050727-3600)
    基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
System.Windows.Forms.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------

************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。

例如:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。

错误图片如下:

因为在项目中使用到了System.WindowsCE.Forms命名空间的InputPanel类,所以怀疑是使用该命名空间编译程序后,不能在桌面操作系统中运行。

因此做了如下测试: 

1.创建一个只有Form页面WinCE项目,在Form下添加命名空间;编译执行,程序报错;

2.将上面用到的代码和System.WindowsCE.Forms命名空间注销后再次编译程序,OK,程序兼容WinCE和桌面系统;

因此断定是该命名空间被编译后影响了在桌面系统的执行,于是想到若是使用API来调用软键盘的显示/隐藏,应该可以避免这个问题。

尝试移除了这个名称空间,使用API调用软键盘的输入法,核心代码如下 :

代码
/// <summary>
/// 显示/隐藏软键盘
/// </summary>
/// <param name="SIP_STATUS"></param>
/// <returns></returns>
[DllImport("coredll", EntryPoint = "SipShowIM")]
private static extern bool SipShowIM(IntPtr SIP_STATUS);
private static readonly IntPtr SIPF_OFF = (IntPtr)0x0;
private static readonly IntPtr SIPF_ON = (IntPtr)0x1;

/// <summary>
/// 显示/隐藏软键盘
/// </summary>
/// <param name="visible">是否显示</param>
/// <returns></returns>
public static bool SipShowIM(bool isShow)
{
if (Platform.Equals("WinCE"))
{
return SipShowIM(isShow ? SIPF_ON : SIPF_OFF);
}
else
{
return false;
}
}

运行后程序不会抛出异常 ,完成程序要求。

总结:

  如果开发兼容桌面系统的WinCE应用程序,做好不要使用单独为移动项目使用的命名空间,因为这样编译出来的程序,只能支持移动设备。

建议使用系统API来完成在移动系统下的功能,当然我这段代码,在桌面系统执行的时候,是不能后弹出软键盘的。这个程序的初衷是兼容WinCE系统和桌面系统,使其能够同时在这两者中运行良好。

原文地址:https://www.cnblogs.com/bobbychencj/p/1884716.html