.Net Maui - LocationWhenInUse permissions using Windows simulator

Viewed 38

I am making use of the following code:

status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();

... before trying to make a call to:

Geolocation.Default.GetLocationAsync()

... when run using the Windows simulator the following exception was raised:

An exception of type 'Microsoft.Maui.ApplicationModel.PermissionException' occurred in System.Private.CoreLib.dll but was not handled in user code
You need to declare the capability `location` in your AppxManifest.xml file

... ok, so I add the following to the file: Platforms\Windows\Package.appxmanifest

<Capabilities>
  ...
  <DeviceCapability Name="location"/>
</Capabilities>

... run again using the Windows simulator and status always returns with a value of PermissionStatus.Denied.

According to the documentation regarding permissions on Windows states:

Permissions must have matching capabilities declared in the package manifest. Permission status defaults to Unknown in most instances.

Question:

How (and where) does one change the "Permission status defaults"?

Thanks in advance.

1 Answers

When using the Windows simulator, you need to change the App Permissions / Location settings on your development machine, as follows:

In the Windows taskbar use the Search option and enter "Location". Select "Location privacy settings" (system settings). Make the following changes:

enter image description here

You can also enable or disable Location permissions for your app as shown below:

enter image description here

When the above settings are changed to an "on" state, calls to Geolocation.Default.GetLocationAsync() will complete successfully.

Hope that helps anyone else as green as me!

Update: 21-Sep-2022 - another Gotcha!:

The above will allow Geolocation.Default.GetLocationAsync() to complete successfuly when using the Windows Simulator ONLY if the request is made on the MainThread (it works on the Android simulator without having to be on the MainThread).

See my other post regarding this issue for further details.

Related