如何在xamarin.forms中的应用程序中打开设置? [英] How to open setting from our application in xamarin.forms?

查看:158
本文介绍了如何在xamarin.forms中的应用程序中打开设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xamarin.forms.(仅在android中解决以下问题)

I am working on xamarin.forms. (Facing below problem only in android)

启动我的应用程序后,它会检查我的GPS位置是打开还是关闭.

When my application is started it checks my GPS location is on or off.

要打开或关闭GPS位置,我正在使用依赖项服务.

To check GPS location on or off I am using dependency service.

public static bool CheckGPSConnection()
        {
            var gpsConnection = DependencyService.Get<IGPSConnectivity>();
            return gpsConnection.CheckGPSConnection();
        }

当我进入应用程序的主页时,我输入了以下代码

When I come to Home page of my application I put following code

if (Device.OS == TargetPlatform.Android)
{
    if (!App.CheckGPSConnection())
    {
        bool answer = await DisplayAlert("Alert", "Would you like to start GPS?", "Yes", "No");
        if (answer)
        {
              Android.App.Application.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings));
        }
    }
}

但这给了我一个例外

{Android.Util.AndroidRuntimeException:从以下位置调用startActivity() 在活动上下文之外需要FLAG_ACTIVITY_NEW_TASK 旗帜.这真的是您想要的吗?在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() /Users/…}中的[0x0000c]

{Android.Util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/…}

我该怎么办?

推荐答案

这是特定于平台的功能,因此您应该为其创建DependencyService.

This is platform specific functionality, so you should create a DependencyService for it.

就像为IGPSConnectivity创建另一个接口一样.例如ISettingsService.

Just like for the IGPSConnectivity create another interface. For example ISettingsService.

public interface ISettingsService
{
    void OpenSettings();
}

然后在Android上像这样实现它:

Then on Android, implement it like this:

public class SettingsServiceAndroid : ISettingsService
{
    public void OpenSettings()
    {
        Xamarin.Forms.Forms.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocat‌​ionSourceSettings));
    }
}

现在,再次从共享的PCL代码中调用它,就像使用GPS连接一样.

Now call it from your shared PCL code, again, just like with the GPS connectivity one.

DependencyService.Get<ISettingsService>().OpenSettings();

由于使用的是DependencyService,因此将注入每个平台的正确实现.因此,不需要if (Device.OS == TargetPlatform.Android)行,除非您出于其他原因当然这么做了.另外,我认为此方法现在已弃用 .现在,您应该从Xamarin.Forms 2.3.4开始使用Device.RuntimePlatform == Device.Android.

Because you are using the DependencyService, the right implementation per platform will be injected. So, there is no need for the if (Device.OS == TargetPlatform.Android) line, unless you did that for another reason of course. Also, I think this method is now deprecated. You should now use Device.RuntimePlatform == Device.Android as of Xamarin.Forms 2.3.4.

这篇关于如何在xamarin.forms中的应用程序中打开设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆