customBarDynamic
Marc CHEVALDONNE 8 months ago
parent e544812c48
commit b0d40697bb

@ -8,6 +8,7 @@
<ResourceDictionary.MergedDictionaries> <ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" /> <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" /> <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
<ResourceDictionary Source="Resources/Themes/DarkTheme.xaml" />
</ResourceDictionary.MergedDictionaries> </ResourceDictionary.MergedDictionaries>
</ResourceDictionary> </ResourceDictionary>
</Application.Resources> </Application.Resources>

@ -7,7 +7,7 @@
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Shell.FlyoutBehavior="Disabled" Shell.FlyoutBehavior="Disabled"
Shell.Padding="0" Shell.Padding="0"
Shell.BackgroundColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" Shell.BackgroundColor="{DynamicResource PrimaryBackgroundColor}"
Title="ex_CustomToolbar"> Title="ex_CustomToolbar">
<ShellContent <ShellContent

@ -6,7 +6,7 @@
<ContentPage.Behaviors> <ContentPage.Behaviors>
<toolkit:StatusBarBehavior <toolkit:StatusBarBehavior
StatusBarColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryDark}}" StatusBarColor="{DynamicResource PrimaryBackgroundColor}"
StatusBarStyle="{AppThemeBinding Light=LightContent, Dark=DarkContent}" /> StatusBarStyle="{AppThemeBinding Light=LightContent, Dark=DarkContent}" />
</ContentPage.Behaviors> </ContentPage.Behaviors>
@ -65,11 +65,26 @@
SemanticProperties.Description="Welcome to dot net Multi platform App U I" /> SemanticProperties.Description="Welcome to dot net Multi platform App U I" />
<Button <Button
BackgroundColor="{DynamicResource PrimaryBackgroundColor}"
x:Name="CounterBtn" x:Name="CounterBtn"
Text="Click me" Text="Click me"
SemanticProperties.Hint="Counts the number of times you click" SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked" Clicked="OnCounterClicked"
HorizontalOptions="Fill" /> HorizontalOptions="Fill" />
<Picker x:Name="picker"
Title="Select a theme"
SelectedIndexChanged="picker_SelectedIndexChanged"
TextColor="{StaticResource PrimaryForegroundColor}"
BackgroundColor="{StaticResource SecondaryBackgroundColor}" >
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Dark</x:String>
<x:String>Light</x:String>
<x:String>Color Blind</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
</VerticalStackLayout> </VerticalStackLayout>
</ScrollView> </ScrollView>
<AbsoluteLayout AbsoluteLayout.LayoutFlags="All" <AbsoluteLayout AbsoluteLayout.LayoutFlags="All"

@ -1,4 +1,6 @@
namespace ex_CustomToolbar; using CommunityToolkit.Maui.Behaviors;
namespace ex_CustomToolbar;
public partial class MainPage : ContentPage public partial class MainPage : ContentPage
{ {
@ -21,6 +23,33 @@ public partial class MainPage : ContentPage
SemanticScreenReader.Announce(CounterBtn.Text); SemanticScreenReader.Announce(CounterBtn.Text);
} }
void picker_SelectedIndexChanged(object source, EventArgs args)
{
string? chosenThemeString = picker.SelectedItem as string;
ResourceDictionary chosenTheme = chosenThemeString switch
{
"Dark" => new DarkTheme(),
"Light" => new LightTheme(),
"Color Blind" => new ColorBlindTheme(),
_ => new LightTheme()
};
ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
if (mergedDictionaries != null)
{
foreach(var dico in mergedDictionaries.Where(d => d is ICustomTheme).ToList())
{
mergedDictionaries.Remove(dico);
}
mergedDictionaries.Add(chosenTheme);
#if ANDROID
CommunityToolkit.Maui.Core.Platform.StatusBar.SetColor((Color)Application.Current.Resources["PrimaryBackgroundColor"]);
#endif
}
}
private void EllipsisClicked(object sender, EventArgs e) private void EllipsisClicked(object sender, EventArgs e)
{ {
secondaryMenu.IsVisible = !secondaryMenu.IsVisible; secondaryMenu.IsVisible = !secondaryMenu.IsVisible;

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ex_CustomToolbar.ColorBlindTheme">
<Color x:Key="PrimaryBackgroundColor">#92CCDC</Color>
<Color x:Key="SecondaryBackgroundColor">#E1C2CE</Color>
<Color x:Key="PrimaryForegroundColor">#163D42</Color>
<Color x:Key="SecondaryForegroundColor">#446E77</Color>
</ResourceDictionary>

@ -0,0 +1,9 @@
namespace ex_CustomToolbar;
public partial class ColorBlindTheme : ResourceDictionary, ICustomTheme
{
public ColorBlindTheme()
{
InitializeComponent();
}
}

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ex_CustomToolbar.DarkTheme">
<Color x:Key="PrimaryBackgroundColor">#2F2883</Color>
<Color x:Key="SecondaryBackgroundColor">#36743B</Color>
<Color x:Key="PrimaryForegroundColor">#96CBEA</Color>
<Color x:Key="SecondaryForegroundColor">#DBCB82</Color>
</ResourceDictionary>

@ -0,0 +1,9 @@
namespace ex_CustomToolbar;
public partial class DarkTheme : ResourceDictionary, ICustomTheme
{
public DarkTheme()
{
InitializeComponent();
}
}

@ -0,0 +1,6 @@
namespace ex_CustomToolbar;
public interface ICustomTheme
{
}

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ex_CustomToolbar.LightTheme">
<Color x:Key="PrimaryBackgroundColor">#96CBEA</Color>
<Color x:Key="SecondaryBackgroundColor">#DBCB82</Color>
<Color x:Key="PrimaryForegroundColor">#2F2883</Color>
<Color x:Key="SecondaryForegroundColor">#36743B</Color>
</ResourceDictionary>

@ -0,0 +1,9 @@
namespace ex_CustomToolbar;
public partial class LightTheme : ResourceDictionary, ICustomTheme
{
public LightTheme()
{
InitializeComponent();
}
}

@ -25,7 +25,7 @@
<ApplicationTitle>ex_CustomToolbar</ApplicationTitle> <ApplicationTitle>ex_CustomToolbar</ApplicationTitle>
<!-- App Identifier --> <!-- App Identifier -->
<ApplicationId>com.companyname.excustomtoolbar</ApplicationId> <ApplicationId>fr.uca.iut.excustomtoolbar</ApplicationId>
<!-- Versions --> <!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>

Loading…
Cancel
Save