2016-02-17 242 views
0

我的工作在VB.NET的Windows Mobile应用程序,在Visual Studio 2005 我想检查我的设备电池的充电,所以我给我的代码形式的负载是这样的:检查电池状态6.5

Imports System.IO 
Imports System.Data 
Imports System.Drawing.Imaging 
Imports System.Configuration 
Imports Microsoft.Win32 
Imports System.Windows.Forms 
Imports Microsoft.VisualBasic 
Imports System 

Dim power As SystemInformation.PowerStatus = SystemInformation.PowerStatus 
Dim percent As Single = power.BatteryLifePercent 
MsgBox("Percent battery life remaining: " & percent * 100) 

但是显示错误

SystemInformation.PowerStatus未定义。

任何人告诉我用来声明的System.Windows.Forms,不SystemInformation变量部分的类型时,我如何能解决这个问题

+0

Visual Studio ** 2005 **?你的意思是* 2015 *?你有没有在你的项目中引用'System.Windows.Forms'?并且适当的'Imports'语句? – Tim

+0

是的,先生,我WAM在Visual Studio 2005的进口System.io 进口System.Data 进口System.Drawing.Imaging 进口System.Configuration 进口的Microsoft.Win32 进口System.Windows工作。形式 进口Microsoft.VisualBasic 进口系统 –

+0

我增加了这么多进口 –

回答

-1

PowerStatus。该生产线

Dim power As SystemInformation.PowerStatus = SystemInformation.PowerStatus

需要被改为:

Dim power As System.Windows.Forms.PowerStatus = SystemInformation.PowerStatus

这为我工作(我在Visual Studio 2013,但不应该有所作为)。

Private Sub ShowBatteryPercent() 
    Dim power As System.Windows.Forms.PowerStatus = SystemInformation.PowerStatus 
    Dim percent As Single = power.BatteryLifePercent 
    MsgBox("Percent battery life remaining: " & percent * 100) 
End Sub 

必须确保您有Imports System.Windows.Forms

+0

先生我有进口System.Windows.Forms但使用此代码时,我得到错误System.Windows.Forms.PowerStatus未定义 –

+0

我正在Visual Studio 2005和开发widnows的应用程序移动设备中心,我想显示设备的电池状态 –

+0

我不相信视觉工作室的版本应该重要。你的项目引用中还列出了'System.Windows.Forms'吗?你正在开发什么版本的.NET框架? – RianBattle

0

忘记家伙们在谈论VS2015。这与在Windows移动设备上运行的紧凑framwework无关!

你需要使用状态,并通知API:在C#这是

using Microsoft.WindowsMobile.Status; // see https://msdn.microsoft.com/en-us/library/microsoft.windowsmobile.status.aspx 
... 
BatteryLevel batteryLevel = SystemState.PowerBatteryStrength; 
BatteryState batteryState = SystemState.PowerBatteryState; 

确保你对目标的Windows Mobile 6.x的!和comapct框架3.5

另一种方法是使用P/Invoke和GetSystemPowerStatusEx。 请参阅How do you get the current battery level in .NET CF 3.5?

如果您未安装并且目标至少为Windows Mobile 6 SDK,您将不会在添加引用中看到Microsoft.WindowsMo​​bile.Status。

如果你想留在PPC2003中,你需要使用GetSystemPowerStatusEx的P/Invoke。

Here是一个很好的关于获得电池状态artcile,如果你明白这一点。还有很多其他网站,不要懒惰,并使用您的互联网搜索引擎。

对不起,但由于MS重命名SDK并重新排列网站而无需查看现有链接,即使在网页和搜索结果中,我也无法找到Windows Mobile 6.5的开发人员工具包(DTK)已被重新命名为Windows Embedded Handheld 6.5)。

+0

我无法下载Microsoft.WindowsMo​​bile.Status ..而无需将此添加到引用中,我无法导入此权利? –

+0

先生,我可以看到目标设备:掌上电脑2003 SE模拟器 –

+0

PocketPC 2003是非常过时的,没有WindowsMo​​bile.Status。请至少安装Windows Mobile 6 SDK或使用本地函数GetSystemPowerStatusEx进行pinvoke – josef