打开位置设置页面或提示用户启用位置

时间:2018-02-01 07:04:44

标签: c# xamarin xamarin.forms

我一直在乱砍我的大脑并徒劳地搜索。我试图找到一种方法来提示用户通过直接进入设置页面或只是在屏幕上点击是切换位置。 我见过的所有代码似乎都不起作用。任何人都有一些有用的东西。一个详细的例子将非常感谢。谢谢。 我是Xamarin开发的新手

我更喜欢在xamarin表单上做一个方法,但从提示Android用户的东西开始,因为对于iOS我没有模拟器

this page or this

3 个答案:

答案 0 :(得分:2)

所以在经过互联网上的几个教程和答案后,我终于找到了完成我想要实现的目标,

我使用依赖服务作为此处指出的答案之一How to open setting from our application in xamarin

为了在平台特定项目中使用它,很少有人没有提到注册界面。

这里是需要它的人的代码

界面: 我打电话给我的界面ILocSettings.cs

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
            }
        }


    }
}

`

答案 1 :(得分:0)

如果您正在谈论让用户授予位置权限,最简单的方法是使用权限插件。这允许您同时请求和检查特定权限。

The permission plugin and full documentation can he found here

然后您就可以执行以下操作:

try
{
    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;
}

答案 2 :(得分:0)

First

Third

图像显示设置页面的屏幕