Geolocator.StatusChanged 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當 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 物件來存取事件的相關資訊。
使用地理柵欄時,請使用 GeofenceMonitor的 StatusChanged 事件來監視位置許可權的變更,而不是 來自 Geolocator 類別的這個事件。 Disabled的GeofenceMonitorStatus相當於DisabledPositionStatus - 兩者都表示應用程式沒有存取位置的許可權。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應