Windows Phone开发教程5Word格式文档下载.docx

上传人:b****4 文档编号:6977772 上传时间:2023-05-07 格式:DOCX 页数:15 大小:955.50KB
下载 相关 举报
Windows Phone开发教程5Word格式文档下载.docx_第1页
第1页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第2页
第2页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第3页
第3页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第4页
第4页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第5页
第5页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第6页
第6页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第7页
第7页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第8页
第8页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第9页
第9页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第10页
第10页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第11页
第11页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第12页
第12页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第13页
第13页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第14页
第14页 / 共15页
Windows Phone开发教程5Word格式文档下载.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Windows Phone开发教程5Word格式文档下载.docx

《Windows Phone开发教程5Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Windows Phone开发教程5Word格式文档下载.docx(15页珍藏版)》请在冰点文库上搜索。

Windows Phone开发教程5Word格式文档下载.docx

5<

TextBlockText="

状态:

"

100"

6<

TextBlockName="

txtblkAccelerometerState"

Text="

停止采集数据"

7<

/StackPanel>

8<

9<

更新加度计数据时间:

10<

txtblkupdateDataAccelerometer"

20ms"

11<

12<

Grid>

13<

TextBlockHeight="

45"

HorizontalAlignment="

Left"

Name="

txtblkX"

X:

1.0"

VerticalAlignment="

Top"

Foreground="

Red"

FontWeight="

Bold"

FontSize="

28"

/>

14<

Center"

txtblkY"

Y:

Green"

15<

Right"

txtblkZ"

Z:

Blue"

16<

/Grid>

17<

GridHeight="

300"

18<

Linex:

xLine"

X1="

240"

Y1="

150"

X2="

340"

Y2="

Stroke="

StrokeThickness="

4"

/Line>

19<

yLine"

60"

20<

zLine"

190"

200"

21<

22<

ButtonContent="

开始采集"

0100"

btnSwitch"

Click="

btnSwitch_Click"

/Button>

23<

24<

复制代码

这是MainPage.xaml.cs后台处理程序:

publicpartialclassMainPage:

PhoneApplicationPage

{

Accelerometeraccelerometer;

DispatcherTimertimer;

Vector3acceleration;

boolisDataValid;

//构造函数

publicMainPage()

InitializeComponent();

if(!

Accelerometer.IsSupported)

txtblkAccelerometerState.Text="

设备不支持加速计"

;

}

else

//初始化timer,并绑定Tick事件

timer=newDispatcherTimer();

timer.Interval=TimeSpan.FromMilliseconds(30);

timer.Tick+=newEventHandler(timer_Tick);

///<

summary>

///开始和停止采集加速计数据

/summary>

paramname="

sender"

/param>

e"

privatevoidbtnSwitch_Click(objectsender,RoutedEventArgse)

if(accelerometer!

=null&

&

accelerometer.IsDataValid)

//停止采集加速计数据

accelerometer.Stop();

timer.Stop();

停止加速度度计"

btnSwitch.Content="

开始采集数据"

if(accelerometer==null)

//实例化一个accelerometer对象

accelerometer=newAccelerometer();

//20毫秒更新一次数据

accelerometer.TimeBetweenUpdates=TimeSpan.FromMilliseconds(20);

//更新传感器数据的时间

txtblkupdateDataAccelerometer.Text=accelerometer.TimeBetweenUpdates.TotalMilliseconds+"

ms"

//从传感器获得新数据时发生

accelerometer.CurrentValueChanged+=newEventHandler<

SensorReadingEventArgs<

AccelerometerReading>

(accelerometer_CurrentValueChanged);

try

accelerometer.Start();

timer.Start();

catch(InvalidOperationException)

无法启动加速计."

voidaccelerometer_CurrentValueChanged(objectsender,SensorReadingEventArgs<

e)

//这个事件处理程序是被主执行线程(UI)之外的线程调用的

//不能直接访问页面上的元素,因为它们位于UI线程中

isDataValid=accelerometer.IsDataValid;

acceleration=e.SensorReading.Acceleration;

voidtimer_Tick(objectsender,EventArgse)

if(isDataValid)

正在从加速计获取数据"

//显示加速计的数值

txtblkX.Text="

"

+acceleration.X.ToString("

0.00"

);

txtblkY.Text="

+acceleration.Y.ToString("

txtblkZ.Text="

+acceleration.Z.ToString("

//使用Line元素显示加速计返回的值

xLine.X2=xLine.X1+acceleration.X*100;

yLine.Y2=yLine.Y1-acceleration.Y*100;

zLine.X2=zLine.X1-acceleration.Z*50;

zLine.Y2=zLine.Y1+acceleration.Z*50;

编译运行上面的程序:

这是运行程序后的效果(没有开始采集数据)   点击开始采集数据后,Y等于-1g,表示手机(模拟器)是纵向竖立的

但这样看好像并不能直观的了解加速计的作用,我们可以通过使用该模拟器的加速计模拟器工具来测试应用程序。

点击模拟器的加速计工具的“播放按钮”或者是拖动加速计工作中手机上的桔红色圆点,模拟手机的各个移动状态。

我们可看到页面的3个分别代表着X/Y/Z的三个Line元素随着加速计的数据变化而不断变化。

2.位置服务(LocationService)  

WindowsPhone应用程序可以通过一种被称为A-GPS(Assisted-GPS,辅助GPS)的技术获取手机当前所在的地理位置。

对手机进行定位所实用的核心类是:

GeocoordinateWatcher。

使用时需要引用System.Device.DLL程序集,并引用System.Device.Location命名空间。

WMAppManifest.xml做如下标记(默认包含):

ID_CAP_LOCTION"

GeocoordinateWatcher的构造方法可以接收一个GetPositionAccuracy枚举类型的参数,该枚举的成员有:

∙Default(默认精度)

∙High(高精度)

GeocoordinateWatcher对象需要注册一个名为:

PositionChanged事件,检测到位置更改时发生。

PostitionChanged事件会传递一个GeoCoordinate对象,该对象有八个属性,分别是:

∙Latitude(纬度),double类型,-90至90之间。

∙Longitude(经度),double类型,-180到180之间

∙Altitude(高度),double类型

∙HorizontalAccuracy(水平精度)和VerticalAccuracy(竖直精度),double类型

∙Course(航向),double类型,0至360之间

∙Speed(速度),double类型

∙IsUnknown,Boolean类型,当Latitude(纬度)和Longitude(经度)为非数字时,则为ture

GeoCoordinate类有一个方法GetDistanceTo,用于计算两个GeoCoordinate对象的距离。

北纬为正值,南纬为负值;

东经为正值,西经为负值。

如果应用程序没有获取用户位置的授权,Latitude(纬度)和Longitude(经度)的值为Double.NaN。

  

现在我们通过一个示例来学习如何使用WindowsPhone中的位置服务。

MainPage.xaml

1    <

StackPanel>

30"

textBlock1"

txtblkLocationState"

位置服务当前不可用"

StackPanel>

500"

经度:

0.000"

txtblkLatitude"

002000"

纬度:

txtblkLongitude"

开始定位"

Height="

72"

btnStartLocation"

Width="

160"

Bottom"

500"

btnStartLocation_Click"

停止定位"

btnStopLocation"

btnStopLocation_Click"

这里是MainPage.xaml.cs后台处理程序:

1publicpartialclassMainPage:

2{

3GeoCoordinateWatcherwatcher;

4//构造函数

5publicMainPage()

6{

7InitializeComponent();

8}

9

10///<

11    ///开始定位

12    ///<

13    ///<

14    ///<

15privatevoidbtnStartLocation_Click(objectsender,RoutedEventArgse)

16{

17if(watcher==null)

18{

19watcher=newGeoCoordinateWatcher(GeoPositionAccuracy.High);

//采用高精度

20watcher.MovementThreshold=20;

//PositionChanged事件之间传送的最小距离

21

22watcher.StatusChanged+=newEventHandler<

GeoPositionStatusChangedEventArgs>

(watcher_StatusChanged);

23watcher.PositionChanged+=newEventHandler<

GeoPositionChangedEventArgs<

GeoCoordinate>

(watcher_PositionChanged);

24

25watcher.Start();

//开始使用位置服务

26}

27else//方便测试,实际使用需要注意

28{

29watcher.Start();

30}

31}

32

33//检测到位置更改时

34//当定位服务已准备就绪并接收数据时,它将开始引发PositionChanged事件

35voidwatcher_PositionChanged(objectsender,GeoPositionChangedEventArgs<

36{

37this.txtblkLatitude.Text=e.Position.Location.Latitude.ToString("

38this.txtblkLongitude.Text=e.Position.Location.Longitude.ToString("

39}

40

41//当位置服务状态发生变化时

42    //在GeoPositionStatusChangedEventArgs对象中传递的GeoPositionStatus枚举获取该服务的当前状态。

43    //可以使用它在应用程序中启用基于位置的功能,以及将服务的当前状态通知给用户。

44voidwatcher_StatusChanged(objectsender,GeoPositionStatusChangedEventArgse)

45{

46switch(e.Status)

47{

48//如果服务的状态为Disabled,则可以检查Permission属性,看用户是否禁用了应用程序的定位服务功能。

49caseGeoPositionStatus.Disabled:

50if(watcher.Permission==GeoPositionPermission.Denied)

51{

52//用户禁用了定位服务

53this.txtblkLocationState.Text="

对位置服务的访问被拒绝。

54}

55else

56{

57this.txtblkLocationState.Text="

设备的定位服务不能够正常使用。

58}

59break;

60caseGeoPositionStatus.Initializing:

61//位置服务正在尝试获取数据

62this.btnStartLocation.IsEnabled=false;

63break;

64caseGeoPositionStatus.NoData:

65this.txtblkLocationState.Text="

当前位置无法进行定位"

66this.btnStopLocation.IsEnabled=true;

67break;

68caseGeoPositionStatus.Ready:

69this.txtblkLocationState.Text="

位置服务已启用,并准备就绪"

70this.btnStopLocation.IsEnabled=true;

71break;

72}

73}

74

75///<

76    ///停止定位

77    ///<

78    ///<

79    ///<

80privatevoidbtnStopLocation_Click(objectsender,RoutedEventArgse)

81{

82watcher.Stop();

83this.btnStartLocation.IsEnabled=true;

84this.txtblkLocationState.Text="

位置服务已经停止"

85}

86}

编译运行程序后,通过模拟器可以模拟当前位置,通过使用模拟器中模拟位置工具变化当前设备所在坐标,我们可以看到程序上的数字也随之不断变化:

参考资料:

  

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2