Theme: instant switching via ThemeChanged event

- Add static App.ThemeChanged event fired after resource dictionary swap
- Pages/controls subscribe OnAppearing and call reload on change
- Themes include Extreme/SecondaryTextColor tokens for dark and light

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Gerwin Kuijntjes
2026-03-31 14:47:03 +02:00
parent 9421582e2c
commit ad1547db6d
4 changed files with 38 additions and 15 deletions

View File

@@ -1,4 +1,6 @@
using System;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using Xamarin.Neo4j.Pages;
@@ -10,15 +12,23 @@ namespace Xamarin.Neo4j
{
public partial class App : Application
{
public static event EventHandler ThemeChanged;
public App()
{
InitializeComponent();
SetTheme(Current.RequestedTheme);
RequestedThemeChanged += (s, e) => SetTheme(e.RequestedTheme);
MainPage = new NavigationPage(new RootPage());
MainPage = new NavigationPage(new ConnectionsPage());
// SetTheme ran before MainPage was assigned, so apply bar colours now.
ApplyNavBarColors();
}
public void UpdateTheme(AppTheme theme) => SetTheme(theme);
private void SetTheme(AppTheme theme)
{
Resources = theme switch
@@ -28,6 +38,20 @@ namespace Xamarin.Neo4j
_ => new LightTheme()
};
ApplyNavBarColors();
ThemeChanged?.Invoke(this, EventArgs.Empty);
}
// Called both from SetTheme and from MainActivity.OnResume so that
// theme changes while the app is backgrounded are picked up on return.
public void ApplyNavBarColors()
{
if (MainPage is NavigationPage navPage)
{
navPage.BarBackgroundColor = Color.FromArgb("#31333b");
navPage.BarTextColor = Colors.White;
}
}
protected override void OnStart()

View File

@@ -31,7 +31,7 @@
<Style TargetType="TabbedPage" ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor" Value="White"/>
<Setter Property="BarTextColor" Value="Black"/>
<Setter Property="BarTextColor" Value="White"/>
<Setter Property="SelectedTabColor" Value="White"/>
</Style>

View File

@@ -4,18 +4,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xamarin.Neo4j.Themes.Fonts">
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeBrands">
<On Platform="Android" Value="FontAwesome5Brands.ttf#Regular" />
<On Platform="iOS" Value="FontAwesome5Brands-Regular" />
</OnPlatform>
<x:String x:Key="FontAwesomeBrands">FontAwesome5Brands-Regular</x:String>
<x:String x:Key="FontAwesomeSolid">FontAwesome5Free-Solid</x:String>
<x:String x:Key="FontAwesomeRegular">FontAwesome5Free-Regular</x:String>
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeSolid">
<On Platform="Android" Value="FontAwesome5Solid.ttf#Regular" />
<On Platform="iOS" Value="FontAwesome5Free-Solid" />
</OnPlatform>
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeRegular">
<On Platform="Android" Value="FontAwesome5Regular.ttf#Regular" />
<On Platform="iOS" Value="FontAwesome5Free-Regular" />
</OnPlatform>
</ResourceDictionary>

View File

@@ -23,6 +23,15 @@
<Setter Property="BackgroundColor" Value="Transparent" />
</Style>
<Style TargetType="Entry" ApplyToDerivedTypes="True">
<Setter Property="TextColor" Value="Black" />
<Setter Property="PlaceholderColor" Value="#868a92" />
</Style>
<Style TargetType="Picker" ApplyToDerivedTypes="True">
<Setter Property="TextColor" Value="Black" />
</Style>
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="{StaticResource Accent}"/>
<Setter Property="BarTextColor" Value="White" />