Share via


Geolocator.StatusChanged 事件

定义

Geolocator 提供更新的位置的能力发生更改时引发。

// Register
event_token StatusChanged(TypedEventHandler<Geolocator, StatusChangedEventArgs const&> const& handler) const;

// Revoke with event_token
void StatusChanged(event_token const* cookie) const;

// Revoke with event_revoker
Geolocator::StatusChanged_revoker StatusChanged(auto_revoke_t, TypedEventHandler<Geolocator, StatusChangedEventArgs const&> const& handler) const;
public event TypedEventHandler<Geolocator,StatusChangedEventArgs> StatusChanged;
function onStatusChanged(eventArgs) { /* Your code */ }
geolocator.addEventListener("statuschanged", onStatusChanged);
geolocator.removeEventListener("statuschanged", onStatusChanged);
- or -
geolocator.onstatuschanged = onStatusChanged;
Public Custom Event StatusChanged As TypedEventHandler(Of Geolocator, StatusChangedEventArgs) 

事件类型

Windows 要求

应用功能
location ID_CAP_LOCATION [Windows Phone]

示例

此代码示例演示如何处理 StatusChanged 事件。 Geolocator 对象触发 StatusChanged 事件以指示用户位置设置已更改。 该事件通过参数的 Status 属性(类型为 PositionStatus)传递相应的状态。 请注意,此方法不是从 UI 线程中调用的,并且 Dispatcher 对象调用了 UI 更改。 有关详细信息,请参阅获取当前位置

using Windows.UI.Core;
...
async private void OnStatusChanged(Geolocator sender, StatusChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        // Show the location setting message only if status is disabled.
        LocationDisabledMessage.Visibility = Visibility.Collapsed;

        switch (e.Status)
        {
            case PositionStatus.Ready:
                // Location platform is providing valid data.
                // notify user: Location platform is ready
                break;

            case PositionStatus.Initializing:
                // Location platform is attempting to acquire a fix. 
                // notify user: Location platform is attempting to obtain a position
                break;

            case PositionStatus.NoData:
                // Location platform could not obtain location data.
                // notify user: Not able to determine the location
                break;

            case PositionStatus.Disabled:
                // The permission to access location data is denied by the user or other policies.
                // notify user: Access to location is denied

                // Clear cached location data if any
                break;

            case PositionStatus.NotInitialized:
                // The location platform is not initialized. This indicates that the application 
                // has not made a request for location data.

                // notify user: No request for location is made yet
                break;

            case PositionStatus.NotAvailable:
                // The location platform is not available on this version of the OS.

                // notify user: Location is not available on this version of the OS
                break;

            default:
                // unknown result
                break;
        }
    });
}

注解

可以使用传递给事件处理程序的 StatusChangedEventArgs 对象访问有关事件的信息。

使用地理围栏时,请使用 GeofenceMonitorStatusChanged 事件监视位置权限的更改,而不是 Geolocator 类中的此事件。 DisabledGeofenceMonitorStatus 等效于 DisabledPositionStatus - 两者都表示应用没有访问位置的权限。

适用于

另请参阅