如何知道Windows Phone 8中的开/关位置?

时间:2014-03-03 17:41:12

标签: c# windows-phone-8 windows-phone-8-emulator

我已尝试使用以下代码检查位置模式的状态,但它对我不起作用。

Geolocator locationservice = new Geolocator();
if (locationservice.LocationStatus == PositionStatus.Disabled)
{
locationButton.Opacity = 0.5;
}

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码,它会从位置服务获取数据,并在位置服务状态发生变化时发出更改通知。此代码来自msdn

using System.Device.Location;

// Click the event handler for the “Start Location” button.
private void startLocationButton_Click(object sender, RoutedEventArgs e)
{
    // The watcher variable was previously declared as type GeoCoordinateWatcher. 
    if (watcher == null)
    {
        watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy
        watcher.MovementThreshold = 20; // use MovementThreshold to ignore noise in the signal
        watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
        watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
    }
    watcher.Start();
}

void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
    switch (e.Status)
    {
        case GeoPositionStatus.Disabled:
            // The Location Service is disabled or unsupported.
            // Check to see whether the user has disabled the Location Service.
            if (watcher.Permission == GeoPositionPermission.Denied)
            {
                // The user has disabled the Location Service on their device.
                statusTextBlock.Text = "you have this application access to location.";
            }
            else
            {
                statusTextBlock.Text = "location is not functioning on this device";
            }
            break;

        case GeoPositionStatus.Initializing:
            // The Location Service is initializing.
            // Disable the Start Location button.
            startLocationButton.IsEnabled = false;
            break;

        case GeoPositionStatus.NoData:
            // The Location Service is working, but it cannot get location data.
            // Alert the user and enable the Stop Location button.
            statusTextBlock.Text = "location data is not available.";
            stopLocationButton.IsEnabled = true;
            break;

        case GeoPositionStatus.Ready:
            // The Location Service is working and is receiving location data.
            // Show the current position and enable the Stop Location button.
            statusTextBlock.Text = "location data is available.";
            stopLocationButton.IsEnabled = true;
            break;
    }
}