1

我有以下场景:一个 Windows Mobile 6.1 (.NET Compact Framwork 2.0) Forms 应用程序(同时在多个设备上运行),它使用来自服务器的多个 WCF 服务。要使用 windows 窗体应用程序,用户必须登录。

服务器使用来自 SAP 系统的数据。WCF 服务器必须根据记录的用户从 SAP 检索一些信息。因此,我的 Windows 窗体应用程序必须(不时)显示它们仍在运行到我的 WCF 服务器。

我能做些什么来完成这项任务?我正在考虑创建一个更新 WCF 服务器的 SQL 服务器的后台任务。(如会话控制)。

4

1 回答 1

1

您可以侦听应用程序中的电源状态更改。文档显示它仅适用于 Windows Mobile 6.5,但我在运行 .NET CF 3.5 的 Windows Mobile 5.0 应用程序中使用它。

就像手机一样,如果移动设备长时间不使用,操作系统会显示电池处于同一水平,直到它真正被使用。所以,不是很靠谱。

但是,您可以侦听事件(如 POWER_STATE_CRITICAL等)并让您的软件进行相应的更改以与您的 WCF 服务器交互。

下面是我使用的东西的编辑版本。

这不会 100% 解决您的问题,但它应该让您了解如何做您需要的事情。

using Microsoft.WindowsMobile.Status;

BatteryLevel _batteryLevel;
BatteryState _batteryState;

void Mobile5_Load(object sender, EventArgs e) {
  _batteryLevel = (BatteryLevel)SystemState.GetValue(SystemProperty.PowerBatteryStrength);
  _batteryState = (BatteryState)SystemState.GetValue(SystemProperty.PowerBatteryState);
  if (!BatteryCritical(false)) {
    // Continue
  }
}
/// <summary>
/// Sets the Battery Level and Battery State for the Mobile Device
/// <para><value>showDialog=True show the dialog box</value></para>
/// <para><value>Returns True <b>if</b> the battery is in a critical state</value></para>
/// </summary>
/// <param name="showDialog">Do you want a dialog box to be displayed or not?</param>
/// <returns>false if the Battery is NOT in the critical state</returns>
bool BatteryCritical(bool showDialog) {
  _batteryAlert = false;
  bool bad = false; // everything starts out ok. We are actually running, after all.
  _batteryLevel = (BatteryLevel)SystemState.GetValue(SystemProperty.PowerBatteryStrength);
  _batteryState = (BatteryState)SystemState.GetValue(SystemProperty.PowerBatteryState);
  bool present = ((_batteryState & BatteryState.NotPresent) != BatteryState.NotPresent);
  bool charging = ((_batteryState & BatteryState.Charging) == BatteryState.Charging);
  bool critical = ((_batteryState & BatteryState.Critical) == BatteryState.Critical);
  bool lowbatry = ((_batteryState & BatteryState.Low) == BatteryState.Low);
  Color c;
  if (present) {
    if (charging) {
      c = Color.Cyan;
    } else {
      if (critical) {
        c = Color.Orange;
        _batteryAlert = true;
      } else if (lowbatry) {
        c = Color.Yellow;
        _batteryAlert = true;
      } else {
        c = Color.White;
      }
    }
  } else {
    c = Color.Silver;
  }
  StatusPanel.BackColor = c;
  switch (_batteryLevel) {
    case BatteryLevel.VeryHigh: // (81-100%)
      BatteryPanel.BackColor = Color.Cyan;
      break;
    case BatteryLevel.High: // (61-80%)
      BatteryPanel.BackColor = Color.Lime;
      break;
    case BatteryLevel.Medium: // 41-60%)
      BatteryPanel.BackColor = Color.Yellow;
      break;
    case BatteryLevel.Low: // (21-40%)
      BatteryPanel.BackColor = Color.Orange;
      //WirelessUpdate(RadioState.Off, false);
      break;
    case BatteryLevel.VeryLow: // (0-20%)
      BatteryPanel.BackColor = Color.Red;
      //WirelessUpdate(RadioState.Off, false);
      bad = (!charging && present);
      break;
  }
  if (showDialog) {
    string msg = string.Format("Level is {0}\r\nState is {1}", _batteryLevel, _batteryState);
    if (_batteryLevel == BatteryLevel.Low) {
      msg += "\r\n\r\nThe wireless radio will be inactive until it has been charged.";
    } else if (bad == true) {
      msg += "\r\n\r\nThis Application will now close to preserve the device. Please return it to a charging base immediately.";
    }
    MessageBox.Show(msg, "Battery Meter", MessageBoxButtons.OKCancel, MessageBoxIcon.None, 0);
  }
  if (!bad) {
    StatusPanel.Refresh();
    // You could signal your app here
  } else {
    // Tell your app this device needs to turn off now.
  }
  return bad;
}
于 2013-03-05T15:47:39.670 回答