在进行WinPhone开发时,想使用手机的GPS定位服务,当然此功能只能在真机调试。
但是在进行真机调试时,想常规应用一样调试时,虽然在设置里开了定位服务,但是还是会遇到问题:
在调用 GeoCoordinateWatcher()时会出现异常:
“System.UnauthorizedAccessException”类型的异常在 System.Device.ni.dll 中发生,但未在用户代码中进行处理
如有适用于此异常的处理程序,该程序便可安全地继续运行。
找了很久原来是程序中没有设置获取定位服务的权限,在微软MSDN找到了解决方法:
-
在“解决方案资源管理器”中,展开“属性”文件夹,然后双击 WMAppManifest.xml。
-
在清单设计器的“功能”选项卡上,选中 ID_CAP_LOCATION 旁边的复选框。
这些选项的介绍,详见MSDN: Windows Phone 8 应用功能和硬件要求。
当勾选ID_CAP_LOCATION全面地复选框,保存之后,再启动调试,就可以正常使用了。
下面附上界面和代码:
界面布局:
代码:
MainPage.xaml
<phone:PhoneApplicationPage
x:Class="GPSTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- 本地化说明:
若要本地化显示的字符串,请将其值复制到应用程序的非特定语言资源文件(AppResources.resx)
中的适当命名的键,然后
将属性的引号之间的硬编码文本值
替换为其路径指向该字符串名称的绑定子句。
例如:
Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
此绑定指向模板的名为“ApplicationTitle”的字符串资源。
在“项目属性”选项卡中添加受支持的语言将会为
每种语言创建一个新的 resx 文件,该文件可以包含 UI 字符串的翻译值
。这些示例中的绑定将导致在运行时从
与应用程序的 CurrentUICulture 匹配的 .resx 文件中
提取属性的值。
-->
<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0">
<Run Text="GPS"/>
<LineBreak/>
<Run/>
</TextBlock>
<TextBlock Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Height="107">
<Run Text="定位服务"/>
<LineBreak/>
<Run/>
</TextBlock>
</StackPanel>
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Margin="12,175,12,24" Grid.RowSpan="2">
<TextBox x:Name="LongitudeTextBox" HorizontalAlignment="Left" Height="72" Margin="108,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="288"/>
<TextBox x:Name="LatitudeTextBox" HorizontalAlignment="Left" Height="72" Margin="108,144,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="288"/>
<TextBox x:Name="StatusTextBox" HorizontalAlignment="Left" Height="72" Margin="108,228,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="288"/>
<Button x:Name="StartButton" Content="启动" HorizontalAlignment="Left" Margin="105,420,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.788,0.473" Click="StartButton_Click"/>
<Button x:Name="StopButton" Content="停止" HorizontalAlignment="Left" Margin="263,420,0,0" VerticalAlignment="Top" Click="StopButton_Click"/>
<TextBlock HorizontalAlignment="Left" Margin="62,85,0,0" TextWrapping="Wrap" Text="经度" VerticalAlignment="Top" RenderTransformOrigin="0.65,3.154"/>
<TextBlock HorizontalAlignment="Left" Margin="62,253,0,0" TextWrapping="Wrap" Text="状态" VerticalAlignment="Top" RenderTransformOrigin="0.387,8.08"/>
<TextBlock HorizontalAlignment="Left" Margin="62,169,0,0" TextWrapping="Wrap" Text="纬度" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" Margin="62,330,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="0.387,8.08">
<Run Text="海拔"/>
<LineBreak/>
<Run/>
</TextBlock>
<TextBox x:Name="AltitudeTextBox" HorizontalAlignment="Left" Height="72" Margin="108,311,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="288"/>
</Grid>
<!--取消注释,以显示对齐网格,从而帮助确保
控件在公用边界上对齐。图像在系统栏中显示时的
上边距为 -32px。如果隐藏了系统栏,则将此值设为 0
(或完全删除边距)。
在发送之前删除此 XAML 和图像本身。-->
<!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
</Grid>
</phone:PhoneApplicationPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GPSTest.Resources;
using System.Device;
using System.Device.Location;
namespace GPSTest
{
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher;
// 构造函数
public MainPage()
{
InitializeComponent();
// 用于本地化 ApplicationBar 的示例代码
//BuildLocalizedApplicationBar();
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
if (watcher == null)
{
//watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
watcher = new GeoCoordinateWatcher();
}
watcher.MovementThreshold = 20;
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
watcher.Start();
}
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
Dispatcher.BeginInvoke(() =>
{
this.LatitudeTextBox.Text = e.Position.Location.Latitude.ToString();
this.LongitudeTextBox.Text = e.Position.Location.Longitude.ToString();
this.AltitudeTextBox.Text = e.Position.Location.Altitude.ToString();
});
}
private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
this.StatusTextBox.Text = e.Status.ToString();
});
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
if (watcher != null)
{
watcher.Stop();
}
}
// 用于生成本地化 ApplicationBar 的示例代码
//private void BuildLocalizedApplicationBar()
//{
// // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
// ApplicationBar = new ApplicationBar();
// // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
// appBarButton.Text = AppResources.AppBarButtonText;
// ApplicationBar.Buttons.Add(appBarButton);
// // 使用 AppResources 中的本地化字符串创建新菜单项。
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
// ApplicationBar.MenuItems.Add(appBarMenuItem);
//}
}
}
注: 本程序代码大部分非原创,只做学习交流,代码版权归原作者所有。