Issue
I want to give the possibility to users to choose my Xamarin.Forms application UI theme. I should change the color values in my application static resources.
<Application.Resources>
<ResourceDictionary x:Name="ColorResources">
<Color x:Key="BgBar">#ffffff</Color>
<Color x:Key="BgHeaderShellMenu">#b3e5fc</Color>
<Color x:Key="BgMenu">#b3e5fc</Color>
<Color x:Key="BgMainPage">#b3e5fc</Color>
<Color x:Key="BgMainPageMenu">#b3e5fc</Color>
<Color x:Key="ForeGroundBar">#82b3c9</Color>
<Color x:Key="SelectedTabBar">#82b3c9</Color>
<Color x:Key="DisabledTabBar">#575757</Color>
<Color x:Key="TtileTabBar">#82b3c9</Color>
<Color x:Key="UnselectedTabBar">#b3e5fc</Color>
<Color x:Key="UnSelectedTabBarLable">#82b3c9</Color>
<Color x:Key="MenuText">#82b3c9</Color>
<Color x:Key="Border">#82b3c9</Color>
<Color x:Key="SelectedTabBarLable">#b3e5fc</Color>
<Color x:Key="MenuLabel">#82b3c9</Color>
<Color x:Key="Primary">#2196F3</Color>
<Color x:Key="Dark">#b3e5fc</Color>
<Style TargetType="Button">
<Setter Property="TextColor" Value="White"></Setter>
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{StaticResource BgBar}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="#332196F3" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
this is my application static resources but, I can't change the values of it because they're all readonly
So I tried this :
private static List<Tuple<string, string>> DarkThemeKVs = new List<Tuple<string, string>>()
{
new Tuple<string, string>("BgBar","#121212"),
new Tuple<string, string>("BgHeaderShellMenu","#121212"),
new Tuple<string, string>("BgMainPage","#121212"),
new Tuple<string, string>("BgMainPageMenu","#121212"),
new Tuple<string, string>("BgMenu","#121212"),
new Tuple<string, string>("ForeGroundBar","#cc2402"),
new Tuple<string, string>("SelectedTabBar","#cc2402"),
new Tuple<string, string>("DisabledTabBar","#575757"),
new Tuple<string, string>("TtileTabBar","#cc2402"),
new Tuple<string, string>("UnselectedTabBar","#121212"),
new Tuple<string, string>("UnSelectedTabBarLable","#cc2402"),
new Tuple<string, string>("MenuText","#cc2402"),
new Tuple<string, string>("Border","#cc2402"),
new Tuple<string, string>("SelectedTabBarLable","#121212"),
new Tuple<string, string>("MenuLabel","#cc2402"),
};
And after initializing components :
if (Theme == "darktheme")
{
ResourceDictionary Rd = new ResourceDictionary();
foreach (var item in this.DarkThemeKVs)
{
Rd.Add(item.Item1, item.Item2);
}
this.Resources = Rd;
}
But I got an exception because values can't be strings. The type of value should be like this :
What should I do? What's the name of this type in c#?
Solution
Add the colors as Color
, and not as string
:
foreach (var item in DarkThemeKVs)
{
Rd.Add(item.Item1, Color.FromHex(item.Item2));
}
Answered By - adamm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.