手机端服务使用指南
立即解锁
发布时间: 2025-08-17 00:57:20 阅读量: 5 订阅数: 7 


Windows Phone 7 开发者指南精华
### 手机端服务使用指南
#### 1. 保存调查答案
在手机端使用服务时,保存调查答案的代码如下:
```csharp
saveSurveyAnswers =
this.surveysServiceClientFactory()
.SaveSurveyAnswers(surveyAnswers)
.Select(
unit =>
{
var sentAnswersCount = surveyAnswers.Count();
surveyStore.DeleteSurveyAnswers(surveyAnswers);
return new TaskCompletedSummary
{
Task = SaveSurveyAnswersTask,
Result = TaskSummaryResult.Success,
Context = sentAnswersCount.ToString()
};
})
.Catch(
(Exception exception) =>
{
…
});
```
此代码通过工厂方法创建服务客户端,调用保存调查答案的方法,统计发送答案数量并删除本地答案,最后返回任务完成摘要。若出现异常,可在 `Catch` 部分进行处理。
#### 2. 使用手机定位服务
Tailspin 希望在用户回答调查时捕获其位置信息,并在同步时将其作为调查数据的一部分发送到服务端。以下是具体分析:
- **解决方案概述**:Windows Phone 7 API 提供了定位服务,但数据准确性与应用功耗存在权衡。Tailspin 不需要高精度定位数据,因此优化应用以降低功耗。应用使用最新可用的位置数据,仅在需要保存调查时请求定位服务。
- **实现细节**:
- **接口与类**:`ILocationService` 接口定义了 `TryToGetCurrentLocation` 方法,`LocationService` 类实现该接口,并使用 `GeoCoordinateWatcher` 类获取位置。
- **代码示例**:
```csharp
private readonly TimeSpan maximumAge = TimeSpan.FromMinutes(15);
private GeoCoordinate lastCoordinate = GeoCoordinate.Unknown;
private DateTime lastCoordinateTime;
private GeoCoordinateWatcher watcher;
private void InitializeWatcher()
{
this.watcher =
new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
this.watcher.Start(true);
if (this.watcher.Status == GeoPositionStatus.Initializing ||
this.watcher.Status == GeoPositionStatus.NoData)
{
this.watcher.StatusChanged += this.WatcherStatusChanged;
}
else
{
this.GetNewLocation();
}
}
private void WatcherStatusChanged(
object sender, GeoPositionStatusChangedEventArgs e)
{
this.GetNewLocation();
}
private void GetNewLocation()
{
if (this.watcher != null)
{
var newCoordinate = this.watcher.Position.Location;
if (newCoordinate != GeoCoordinate.Unknown)
{
this.lastCoordinate = this.watcher.Position.Location;
this.lastCoordinateTime = DateTime.Now;
}
this.DisposeWatcher();
}
}
private void DisposeWatcher()
{
if (this.watcher != null)
{
var oldWatcher = this.watcher;
this.watcher = null;
oldWatcher.Stop();
oldWatcher.Dispose();
}
}
public GeoCoordinate TryToGetCurrentLocation()
{
if (!this.settingsStore.LocationServiceAllowed)
{
return GeoCoordinate.Unknown;
}
if (this.watcher == null)
{
if (this.maximumAge <
(DateTime.Now - this.lastCoordinateTime) ||
this.lastCoordinate == GeoCoordinate.Unknown)
{
this.InitializeWatcher();
}
}
return this.lastCoordinate;
}
```
以下是获取定位数据的流程图:
```mermaid
graph TD
A[开始] --> B{是否允许定位服务}
B -- 是 --> C{是否需要新数据}
B -- 否 --> D[返回未知坐标]
C -- 是 --> E[初始化定位器]
C -- 否 --> F[返回上次坐标]
E --> G{定位器状态}
G -- 初始化或无数据 --> H[监听状态变化]
G -- 其他 --> I[获取新位置]
H --> I
I --> J{新坐标是否有效}
J -- 是 -->
```
0
0
复制全文
相关推荐










