繁体   English   中英

使用 Xamarin.Forms 中的 C# 为资源添加颜色

[英]Add color to resources with C# in Xamarin.Forms

我想让用户可以选择我的 Xamarin.Forms 应用程序 UI 主题。 我应该更改应用程序 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>

这是我的应用程序 static 资源,但是我无法更改它的值,因为它们都是readonly

所以我尝试了这个:

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"),
    
    };

在初始化组件之后:

if (Theme == "darktheme")
            {
                ResourceDictionary Rd = new ResourceDictionary();
                foreach (var item in this.DarkThemeKVs)
                {
                    Rd.Add(item.Item1, item.Item2);
                }
                this.Resources = Rd;
            }

但我有一个例外,因为值不能是字符串。 值的类型应该是这样的: 在此处输入图像描述

我应该怎么办? c#中这种类型的名称是什么?

将 colors 添加为Color ,而不是string

            foreach (var item in DarkThemeKVs)
            {
                Rd.Add(item.Item1, Color.FromHex(item.Item2));
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM