我们可以使用Xamarin.Forms依赖项服务从iOS和Android访问特定于平台的代码。
我在此博客文章中对此进行了更深入的介绍:https : //codetraveler.io/2019/09/10/check-for-dark-mode-in-xamarin-forms/
using System.Threading.Tasks;
namespace MyNamespace
{
public interface IEnvironment
{
Task GetOperatingSystemTheme();
}
public enum Theme { Light, Dark }
}
using Xamarin.Forms;
namespace MyNamespace
{
public App : Application
{
// ...
protected override async void OnStart()
{
base.OnStart();
Theme theme = await DependencyService.Get().GetOperatingSystemTheme();
SetTheme(theme);
}
protected override async void OnResume()
{
base.OnResume();
Theme theme = await DependencyService.Get().GetOperatingSystemTheme();
SetTheme(theme);
}
void SetTheme(Theme theme)
{
//Handle Light Theme & Dark Theme
}
}
}
using System;
using UIKit;
using Xamarin.Forms;
using MyNamespace;
using MyNamespace.iOS;
[assembly: Dependency(typeof(Environment_iOS))]
namespace MyNamespace.iOS
{
public class Environment_iOS : IEnvironment
{
public async Task GetOperatingSystemTheme()
{
//Ensure the current device is running 12.0 or higher, because `TraitCollection.UserInterfaceStyle` was introduced in iOS 12.0
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
var currentUIViewCOntroller= await GetVisibleViewController();
var userInterfaceStyle = currentUIViewController.TraitCollection.UserInterfaceStyle;
switch (userInterfaceStyle)
{
case UIUserInterfaceStyle.Light:
return Theme.Light;
case UIUserInterfaceStyle.Dark:
return Theme.Dark;
default:
throw new NotSupportedException($"UIUserInterfaceStyle {userInterfaceStyle} not supported");
}
}
else
{
return Theme.Light;
}
}
static Task GetVisibleViewController()
{
// UIApplication.SharedApplication can only be referenced on by Main Thread, so we'll use Device.InvokeOnMainThreadAsync which was introduced in Xamarin.Forms v4.2.0
return Device.InvokeOnMainThreadAsync(() =>
{
var rootCOntroller= UIApplication.SharedApplication.KeyWindow.RootViewController;
switch (rootController.PresentedViewController)
{
case UINavigationController navigationController:
return navigationController.TopViewController;
case UITabBarController tabBarController:
return tabBarController.SelectedViewController;
case null:
return rootController;
default:
return rootController.PresentedViewController;
}
}
}
}
}
using System;
using System.Threading.Tasks;
using Android.Content.Res;
using Plugin.CurrentActivity;
using Xamarin.Forms;
using MyNamespace;
using MyNamespace.Android;
[assembly: Dependency(typeof(Environment_Android))]
namespace MyNamespace.Android
{
public class Environment_Android : IEnvironment
{
public Task GetOperatingSystemTheme()
{
//Ensure the device is running Android Froyo or higher because UIMode was added in Android Froyo, API 8.0
if(Build.VERSION.SdkInt >= BuildVersionCodes.Froyo)
{
var uiModeFlags = CrossCurrentActivity.Current.AppContext.Resources.Configuration.UiMode & UiMode.NightMask;
switch(uiModeFlags)
{
case UiMode.NightYes:
return Task.FromResult(Theme.Dark);
case UiMode.NightNo:
return Task.FromResult(Theme.Light);
default:
throw new NotSupportedException($"UiMode {uiModeFlags} not supported");
}
}
else
{
return Task.FromResult(Theme.Light);
}
}
}
}