[英]Add color to resources with C# in Xamarin.Forms
I want to give the possibility to users to choose my Xamarin.Forms application UI theme.我想让用户可以选择我的 Xamarin.Forms 应用程序 UI 主题。 I should change the color values in my application static resources.我应该更改应用程序 static 资源中的颜色值。
<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
这是我的应用程序 static 资源,但是我无法更改它的值,因为它们都是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#? c#中这种类型的名称是什么?
Add the colors as Color
, and not as string
:将 colors 添加为Color
,而不是string
:
foreach (var item in DarkThemeKVs)
{
Rd.Add(item.Item1, Color.FromHex(item.Item2));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.