using System;
using System.Collections.Generic;
using System.Text;
[assembly: Xamarin.Forms.Dependency(typeof(DE2.ILocSettings))]
namespace DE2
{
public interface ILocSettings
{
void OpenSettings();
}
这个表单上有一个按钮,我称其为DataEntryForm。
DataEntryForm.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DE2.DataEntryForm"
>
<ContentPage.Content>
<StackLayout>
<Button x:Name="TurnLocationOn"
Text="Turn On Location"
Clicked="TurnLocationOn_OnClicked"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
然后是DataEntryForm.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Plugin.pbXSettings;
using Plugin.Geolocator;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using Plugin.DeviceInfo;
using Plugin.DeviceInfo.Abstractions;
[assembly: Xamarin.Forms.Dependency(typeof(DE2.ILocSettings))]
namespace DE2
{
using Xamarin.Forms.PlatformConfiguration;
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DataEntryForm : ContentPage
{
public DataEntryForm ()
{
InitializeComponent ();
}
private async void TurnLocationOn_OnClicked(object sender, global::System.EventArgs e)
{
var myAction = await DisplayAlert("Location", "Please Turn On Location", "OK","CANCEL");
if (myAction)
{
if (Device.RuntimePlatform == global::Xamarin.Forms.Device.Android)
{
//DependencyService.Get<ISettingsService>().OpenSettings();
global::Xamarin.Forms.DependencyService.Get<global::DE2.ILocSettings>().OpenSettings();
}
else
{
DisplayAlert("Device", "You are using some other shit", "YEP");
}
}
else
{
DisplayAlert("Alert","User Denied Permission","OK");
}
//
}
}
}
然后我将这个类放置在Android特定平台上的LocationZ.cs中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Locations;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Android;
using Xamarin.Forms;
using DE2;
using DE2.Droid;
//[assembly: Xamarin.Forms.Dependency(typeof(ILocSettings))]
//Notice the use of LocationZ in registering below instead of ILocSettings
[assembly: Xamarin.Forms.Dependency(typeof(LocationZ))]
namespace DE2.Droid
{
using System.Runtime.Remoting.Messaging;
using Android.Support.V4.View;
using Android.Support.V7.App;
using Xamarin.Forms;
using DE2;
public class LocationZ : ILocSettings
{
public void OpenSettings()
{
LocationManager LM = (LocationManager)Forms.Context.GetSystemService(Context.LocationService);
if (LM.IsProviderEnabled(LocationManager.GpsProvider)==false)
{
Context ctx = Forms.Context;
ctx.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings));
}
else
{
//this is handled in the PCL
}
}
}
}
`
Android.App.Application.Context
替换Forms.Context
,因为Forms.Context
已经过时。 - Josephtry
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (status != PermissionStatus.Granted)
{
if(await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
{
await DisplayAlert("Need location", "Gunna need that location", "OK");
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
//Best practice to always check that the key exists
if(results.ContainsKey(Permission.Location))
status = results[Permission.Location];
}
if (status == PermissionStatus.Granted)
{
var results = await CrossGeolocator.Current.GetPositionAsync(10000);
LabelGeolocation.Text = "Lat: " + results.Latitude + " Long: " + results.Longitude;
}
else if(status != PermissionStatus.Unknown)
{
await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
}
}
catch (Exception ex)
{
LabelGeolocation.Text = "Error: " + ex;
}