Переключить Android темный режим в настройках устройства из приложения Xamarin? - PullRequest
1 голос
/ 19 июня 2020

Я новичок в мобильной разработке и все еще изучаю ограничения. Можно ли изменить DarkMode в настройках устройства Android, и если да, то как мне это изменить?

Приведенный ниже код представляет собой упрощенную версию моего приложения. Когда вызывается метод EnableSwitchChanged (), я хотел бы переключить значение параметра DarkMode в настройках устройств.

activity_main. xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:padding="25px"
    android:id="@+id/relativeLayout1">

    <LinearLayout
        android:id="@+id/linearLayout1-1"
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="71.0dp">
        <TextView
            android:id="@+id/textView1"
            android:text="Enable"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="270.0dp"
            android:layout_height="match_parent"
            android:gravity="center_vertical|left"
        />
        <Switch
            android:id="@+id/enableSwitch"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
        />
    </LinearLayout>
</RelativeLayout>

MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;

namespace DarkModeToggler
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {

        protected Switch enableSwitch;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            enableSwitch = FindViewById<Switch>(Resource.Id.enableSwitch);
            enableSwitch.CheckedChange += delegate { EnableSwitchChanged(); };
        }

        protected void EnableSwitchChanged()
        {
            string value = enableSwitch.Checked ? "on" : "off";
            string toast = string.Format("Toggle DarkMode from settings of the device to {0}", value);
            Toast.MakeText(this, toast, ToastLength.Long).Show();
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

enter image description here

...