Loading...
Loading...
Compare original and translation side by side
Plugin.Maui.BootstrapThemePlugin.Maui.BootstrapThemeAppThemeBindingResourceDictionaryDynamicResourceAppThemeBindingApplication.Current.RequestedThemeRequestedThemeChangedPreferences.SetPreferences.GetConfigChanges.UiModeMainActivityAppThemeBindingResourceDictionaryDynamicResourceAppThemeBindingApplication.Current.RequestedThemeRequestedThemeChangedPreferences.SetPreferences.GetMainActivityConfigChanges.UiMode| Approach | Best for | Limitation |
|---|---|---|
| AppThemeBinding | Automatic light/dark with OS — minimal code | Only two themes (light + dark) |
| ResourceDictionary swap | Custom branded themes, more than two themes, user preference | More setup; must use |
| Both combined | OS-driven response plus custom theme colors | Most flexible but most complex |
| 方案 | 最佳适用场景 | 局限性 |
|---|---|---|
| AppThemeBinding | 随系统自动切换明暗模式——代码量极少 | 仅支持两种主题(浅色+深色) |
| ResourceDictionary切换 | 自定义品牌主题、多主题支持、用户偏好设置 | 设置工作量更大;必须全程使用 |
| 两者结合 | 系统驱动响应+自定义主题颜色 | 灵活性最高但复杂度也最高 |
AppThemeBindingLightDarkDefaultAppThemeBindingLightDarkDefault<Label Text="Themed text"
TextColor="{AppThemeBinding Light=Green, Dark=Red}"
BackgroundColor="{AppThemeBinding Light=White, Dark=Black}" />
<!-- With resource references -->
<Label TextColor="{AppThemeBinding Light={StaticResource LightPrimary},
Dark={StaticResource DarkPrimary}}" /><Label Text="主题化文本"
TextColor="{AppThemeBinding Light=Green, Dark=Red}"
BackgroundColor="{AppThemeBinding Light=White, Dark=Black}" />
<!-- 结合资源引用 -->
<Label TextColor="{AppThemeBinding Light={StaticResource LightPrimary},
Dark={StaticResource DarkPrimary}}" />var label = new Label();
// Color-specific helper
label.SetAppThemeColor(Label.TextColorProperty, Colors.Green, Colors.Red);
// Generic helper for any bindable property type
label.SetAppTheme<Color>(Label.TextColorProperty, Colors.Green, Colors.Red);var label = new Label();
// 颜色专用辅助方法
label.SetAppThemeColor(Label.TextColorProperty, Colors.Green, Colors.Red);
// 适用于任何可绑定属性类型的通用辅助方法
label.SetAppTheme<Color>(Label.TextColorProperty, Colors.Green, Colors.Red);ResourceDictionaryResourceDictionaryx:ClassInitializeComponent()Sourcex:Class<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Themes.LightTheme">
<Color x:Key="PageBackgroundColor">White</Color>
<Color x:Key="PrimaryTextColor">#333333</Color>
<Color x:Key="AccentColor">#2196F3</Color>
</ResourceDictionary>namespace MyApp.Themes;
public partial class LightTheme : ResourceDictionary
{
public LightTheme() => InitializeComponent();
}x:ClassInitializeComponent()Sourcex:Class<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Themes.LightTheme">
<Color x:Key="PageBackgroundColor">White</Color>
<Color x:Key="PrimaryTextColor">#333333</Color>
<Color x:Key="AccentColor">#2196F3</Color>
</ResourceDictionary>namespace MyApp.Themes;
public partial class LightTheme : ResourceDictionary
{
public LightTheme() => InitializeComponent();
}DynamicResource<ContentPage BackgroundColor="{DynamicResource PageBackgroundColor}">
<Label Text="Hello"
TextColor="{DynamicResource PrimaryTextColor}" />
<Button Text="Action"
BackgroundColor="{DynamicResource AccentColor}" />
</ContentPage>DynamicResource<ContentPage BackgroundColor="{DynamicResource PageBackgroundColor}">
<Label Text="Hello"
TextColor="{DynamicResource PrimaryTextColor}" />
<Button Text="操作"
BackgroundColor="{DynamicResource AccentColor}" />
</ContentPage>void ApplyTheme(ResourceDictionary theme)
{
// Assumes theme dictionaries are the only merged dictionaries.
// If your App.xaml merges non-theme dictionaries (e.g., converters),
// move them to Application.Resources directly instead.
var mergedDictionaries = Application.Current!.Resources.MergedDictionaries;
mergedDictionaries.Clear();
mergedDictionaries.Add(theme);
}
// Usage
ApplyTheme(new DarkTheme());void ApplyTheme(ResourceDictionary theme)
{
// 假设主题字典是唯一合并的字典。
// 如果你的App.xaml合并了非主题字典(例如转换器),请将它们直接移至Application.Resources中。
var mergedDictionaries = Application.Current!.Resources.MergedDictionaries;
mergedDictionaries.Clear();
mergedDictionaries.Add(theme);
}
// 使用示例
ApplyTheme(new DarkTheme());AppTheme currentTheme = Application.Current!.RequestedTheme;
// Returns AppTheme.Light, AppTheme.Dark, or AppTheme.UnspecifiedAppTheme currentTheme = Application.Current!.RequestedTheme;
// 返回AppTheme.Light、AppTheme.Dark或AppTheme.Unspecified// Force dark mode regardless of OS setting
Application.Current!.UserAppTheme = AppTheme.Dark;
// Reset to follow system theme
Application.Current!.UserAppTheme = AppTheme.Unspecified;// 强制深色模式,忽略系统设置
Application.Current!.UserAppTheme = AppTheme.Dark;
// 重置为跟随系统主题
Application.Current!.UserAppTheme = AppTheme.Unspecified;Application.Current!.RequestedThemeChanged += (s, e) =>
{
AppTheme newTheme = e.RequestedTheme;
// Update UI or switch ResourceDictionaries
};Application.Current!.RequestedThemeChanged += (s, e) =>
{
AppTheme newTheme = e.RequestedTheme;
// 更新UI或切换ResourceDictionary
};AppThemeBindingDynamicResource<Label TextColor="{AppThemeBinding
Light={DynamicResource LightPrimary},
Dark={DynamicResource DarkPrimary}}" />Application.Current!.RequestedThemeChanged += (s, e) =>
{
ApplyTheme(e.RequestedTheme == AppTheme.Dark
? new DarkTheme()
: new LightTheme());
};AppThemeBindingDynamicResource<Label TextColor="{AppThemeBinding
Light={DynamicResource LightPrimary},
Dark={DynamicResource DarkPrimary}}" />Application.Current!.RequestedThemeChanged += (s, e) =>
{
ApplyTheme(e.RequestedTheme == AppTheme.Dark
? new DarkTheme()
: new LightTheme());
};Preferences// Save choice
Preferences.Set("AppTheme", "Dark");
// Restore on startup (in App constructor or CreateWindow)
var saved = Preferences.Get("AppTheme", "System");
Application.Current!.UserAppTheme = saved switch
{
"Light" => AppTheme.Light,
"Dark" => AppTheme.Dark,
_ => AppTheme.Unspecified
};Preferences// 保存选择
Preferences.Set("AppTheme", "Dark");
// 启动时恢复(在App构造函数或CreateWindow中)
var saved = Preferences.Get("AppTheme", "System");
Application.Current!.UserAppTheme = saved switch
{
"Light" => AppTheme.Light,
"Dark" => AppTheme.Dark,
_ => AppTheme.Unspecified
};MainActivityConfigChanges.UiMode[Activity(Theme = "@style/Maui.SplashTheme",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize
| ConfigChanges.Orientation
| ConfigChanges.UiMode // ← Required for theme detection
| ConfigChanges.ScreenLayout
| ConfigChanges.SmallestScreenSize
| ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity { }UiModeMainActivityConfigChanges.UiMode[Activity(Theme = "@style/Maui.SplashTheme",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize
| ConfigChanges.Orientation
| ConfigChanges.UiMode // ← 主题检测必需
| ConfigChanges.ScreenLayout
| ConfigChanges.SmallestScreenSize
| ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity { }UiModeDynamicResource<!-- ✅ Updates when theme dictionary changes -->
<Label TextColor="{DynamicResource PrimaryTextColor}" />
<!-- ❌ Frozen at first load — won't update on theme switch -->
<Label TextColor="{StaticResource PrimaryTextColor}" />DynamicResource<!-- ✅ 主题字典变化时会更新 -->
<Label TextColor="{DynamicResource PrimaryTextColor}" />
<!-- ❌ 首次加载后固定不变——主题切换时不会更新 -->
<Label TextColor="{StaticResource PrimaryTextColor}" /><!-- ❌ Will not change with theme -->
<Label TextColor="#333333" />
<!-- ✅ Theme-aware -->
<Label TextColor="{DynamicResource PrimaryTextColor}" /><!-- ❌ 不会随主题变化 -->
<Label TextColor="#333333" />
<!-- ✅ 支持主题感知 -->
<Label TextColor="{DynamicResource PrimaryTextColor}" />x:Keyx:Key| Platform | Minimum Version |
|---|---|
| iOS | 13+ |
| Android | 10+ (API 29) |
| macOS Catalyst | 10.15+ |
| Windows | 10+ |
| 平台 | 最低版本 |
|---|---|
| iOS | 13+ |
| Android | 10+(API 29) |
| macOS Catalyst | 10.15+ |
| Windows | 10+ |
AppThemeBindingSetAppThemeColor()SetAppTheme<T>()Application.Current.RequestedThemeApplication.Current.UserAppTheme = AppTheme.DarkRequestedThemeChangedResourceDictionaryMergedDictionariesDynamicResourceStaticResourcePreferences.SetPreferences.GetAppThemeBindingSetAppThemeColor()SetAppTheme<T>()Application.Current.RequestedThemeApplication.Current.UserAppTheme = AppTheme.DarkRequestedThemeChangedMergedDictionariesResourceDictionaryDynamicResourceStaticResourcePreferences.SetPreferences.Get