在 Windows Phone 8 中跟踪位置时出现异常
Posted
技术标签:
【中文标题】在 Windows Phone 8 中跟踪位置时出现异常【英文标题】:Exception while tracking location in Windows Phone 8 【发布时间】:2013-09-11 21:51:19 【问题描述】:当我尝试跟踪位置时,它工作得很好,但是当我向它添加服务引用时,它会引发异常 当我在不添加位置的情况下尝试相同的程序时,只添加服务引用它完美地工作 我的代码在下面,同时从How to continuously track the phone's location for Windows Phone 8复制而来
public partial class MainPage : PhoneApplicationPage
Geolocator geolocator = null;
bool tracking = false;
ServiceReference2.GetPositionClient client = new ServiceReference2.GetPositionClient();
// Constructor
public MainPage()
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
// User has opted in or out of Location
return;
else
MessageBoxResult result =
MessageBox.Show("This app accesses your phone's location. Is that ok?",
"Location",
MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
else
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
IsolatedStorageSettings.ApplicationSettings.Save();
private void TrackLocation_Click(object sender, RoutedEventArgs e)
if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] != true)
// The user has opted out of Location.
return;
if (!tracking)
geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
geolocator.MovementThreshold = 100; // The units are meters.
geolocator.StatusChanged += geolocator_StatusChanged;
geolocator.PositionChanged += geolocator_PositionChanged;
tracking = true;
TrackLocationButton.Content = "stop tracking";
else
geolocator.PositionChanged -= geolocator_PositionChanged;
geolocator.StatusChanged -= geolocator_StatusChanged;
geolocator = null;
tracking = false;
TrackLocationButton.Content = "track location";
StatusTextBlock.Text = "stopped";
void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
string status = "";
switch (args.Status)
case PositionStatus.Disabled:
// the application does not have the right capability or the location master switch is off
status = "location is disabled in phone settings";
break;
case PositionStatus.Initializing:
// the geolocator started the tracking operation
status = "initializing";
break;
case PositionStatus.NoData:
// the location service was not able to acquire the location
status = "no data";
break;
case PositionStatus.Ready:
// the location service is generating geopositions as specified by the tracking parameters
status = "ready";
break;
case PositionStatus.NotAvailable:
status = "not available";
// not used in WindowsPhone, Windows desktop uses this value to signal that there is no hardware capable to acquire location information
break;
case PositionStatus.NotInitialized:
// the initial state of the geolocator, once the tracking operation is stopped by the user the geolocator moves back to this state
break;
Dispatcher.BeginInvoke(() =>
StatusTextBlock.Text = status;
);
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
client.getPosCompleted += new EventHandler<ServiceReference2.getPosCompletedEventArgs>(sendData);
client.getPosAsync(11,11);
Dispatcher.BeginInvoke(() =>
LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString("0.00");
LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString("0.00");
);
public void sendData(object sender, ServiceReference2.getPosCompletedEventArgs e)
dd.Text = e.Result;
【问题讨论】:
它会抛出什么异常? 【参考方案1】:你有
client.getPosCompleted += new EventHandler<ServiceReference2.getPosCompletedEventArgs>(sendData);
但您没有在其他任何地方为 Client 提供任何值,我假设您收到 null Reference 异常,这就是原因。
【讨论】:
client.getPosCompleted += new EventHandler刚刚解决了,是IIS设置的问题,因为手机和电脑在不同的网络,所以无法通信。我只是在路由器设置中转发端口-
【讨论】:
以上是关于在 Windows Phone 8 中跟踪位置时出现异常的主要内容,如果未能解决你的问题,请参考以下文章
Windows Phone 8.1 - 从后台任务创建 WriteableBitmap 时出现异常
在 HelloWorld Windows Phone 8.1 应用程序中使用 ContactPicker 时出现“设备未准备好(HRESULT 异常:0x80070015)”错误
在Windows Phone 8.1模拟器上从Store安装应用程序时出现错误80070002