C# 如何在Xamarin.Forms中打开/关闭飞行模式?

C# 如何在Xamarin.Forms中打开/关闭飞行模式?,c#,android,xamarin.forms,mobile-data,C#,Android,Xamarin.forms,Mobile Data,如何在Xamarin.Forms for android和ios中打开/关闭飞行模式?正如@Patrick Hofman和@DavidG所说。您无法通过项目中的代码打开/关闭飞行模式。但是,您可以使用依赖服务在表单中打开系统的设置页面,并允许用户打开/关闭飞机模式手册 在窗体中,添加一个新接口 在iOS中 注: App Prefs:是iOS中的专用api。所以如果你想上传到应用商店,它可能会被商店拒绝 在Android中 你是说你想从你的应用程序中设置/取消设置它?不可能。想象一下你被允许这样做

如何在Xamarin.Forms for android和ios中打开/关闭飞行模式?

正如@Patrick Hofman和@DavidG所说。您无法通过项目中的代码打开/关闭飞行模式。但是,您可以使用依赖服务在表单中打开系统的设置页面,并允许用户打开/关闭飞机模式手册

在窗体中,添加一个新接口

在iOS中

注:

App Prefs:是iOS中的专用api。所以如果你想上传到应用商店,它可能会被商店拒绝

在Android中


你是说你想从你的应用程序中设置/取消设置它?不可能。想象一下你被允许这样做,那将是多么糟糕的安全漏洞。
public interface ISettingsService
{
  void OpenSettings();
}
using xxx.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(OpenSettingsImplement))]
namespace xxx.iOS
{
    public class OpenSettingsImplement : ISettingsService
    {
        public void OpenSettings()
        {
            var url = new NSUrl($"App-Prefs:");
            UIApplication.SharedApplication.OpenUrl(url);
        }
    }
}
using Android.Content;

using xxx;
using xxx.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(OpenSettingsImplement))]
namespace xxx.Droid
{
    public class OpenSettingsImplement : ISettingsService
    {
        public void OpenSettings()
        {
            Intent intentOpenSettings = new Intent();
            intentOpenSettings.SetAction(Android.Provider.Settings.ActionAirplaneModeSettings);
            Android.App.Application.Context.StartActivity(intentOpenSettings);
        }
    }
}