在运行时更改xamarin.forms颜色

时间:2016-08-18 21:20:31

标签: c# xamarin themes xamarin.forms

我正在使用xamarin.forms创建一个应用程序,我已经设置了一个颜色方案,我希望能够在设置中更改为黑暗风格或浅色风格现在它都可以工作除了我必须重新启动我选择不同的配色方案后,每次应用程序。

这是我试图在运行时更改它的地方

   private void DarkThemeClick(object sender, EventArgs e)
    {
        database.DropTable(new StyleModel());
        database.CreateTable(new StyleModel());
        database.SaveItem(new StyleModel() { ThemeNum = 1 });
        App.ActiveStyle = new DarkStyle();
    }

    private void LightThemeClick(object sender, EventArgs e)
    {
        database.DropTable(new StyleModel());
        database.CreateTable(new StyleModel());
        database.SaveItem(new StyleModel() { ThemeNum = 0 });
        App.ActiveStyle = new LightStyle();
    }

这是我正在使用的项目的示例,我想要更改

上的颜色
    using System;
using TestXamForms.Style;
using Xamarin.Forms;

namespace TestXamForms.Helpers
{
class EntryValueCell : StackLayout
{

    public EntryValueCell(string key,int FieldIdx, string value = "",  bool isNumber = false)
    {
        Entry entry;
        Label label = new Label()
        {
            TextColor = App.ActiveStyle.LabelTextColor,
            Text = key,
            HorizontalOptions = LayoutOptions.End
        };
        if (isNumber)
        {
            entry = new Entry()
            {
                ClassId = FieldIdx.ToString(),
                TextColor = App.ActiveStyle.LabelTextColor,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Keyboard = Keyboard.Numeric,
                Text = value, 


            };
        }
        else
        {
            entry = new Entry()
            {
                ClassId = FieldIdx.ToString(),
                TextColor = App.ActiveStyle.LabelTextColor,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Keyboard = Keyboard.Text,
                Text = value
            };
        }

        BackgroundColor = App.ActiveStyle.StackLayoutBackground;
        Orientation = StackOrientation.Horizontal;
        VerticalOptions = LayoutOptions.FillAndExpand;
        Children.Add(label);
        Children.Add(entry);
    }
}
}

这是一个配色方案的例子

    using Xamarin.Forms;

   namespace TestXamForms.Style
 {
     public  class LightStyle : StyleBase
    {
       public LightStyle()
        {
        LabelTextColor = Color.Black;
        ButtonColor = Color.FromHex("337ab7");
        StackLayoutBackground = Color.FromHex("eff0f1");
        InputBackgroundColor = Color.White;
        PlaceHolderColor = Color.Gray;
        TableColor = Color.FromHex("e6e6e6");
        StacklayoutBorderColor = Color.Black;
       }
      }
     }

这里是上面文件继承的styleBase

using TestXamForms.Models;
using Xamarin.Forms;

namespace TestXamForms.Style
{
public class StyleBase : ModelBase
{
    public enum ThemeNum : int
    {
        Light = 0, Dark = 1
    }
    public Color LabelTextColor { get; set; }
    public Color ButtonColor { get; set; }
    public Color StackLayoutBackground { get; set; }
    public Color InputBackgroundColor { get; set; }
    public Color PlaceHolderColor { get; set; }

    public Color StacklayoutBorderColor { get; set; }

    public Color TableColor { get; set; }

    public int ThemeNums { get; set; }

}
}

这是App.cs文件的一部分,它在应用程序启动时加载颜色方案

    static StyleBase activeStyle { get; set; }


    public static StyleBase ActiveStyle
    {
        get
        {
            if (activeStyle == null)
            {
                StyleModel styleBase = database.GetItems(new     StyleModel()).First();
                if (styleBase == null)
                {
                    database.SaveItem(new StyleModel() { ThemeNum = 0 }); //sets the default color scheme to light style 
                    styleBase = database.GetItems(new StyleModel()).First();
                }
                int themeNum = styleBase.ThemeNum;
                switch (themeNum)
                {
                    case (int)StyleBase.ThemeNum.Dark:
                        activeStyle = new DarkStyle();
                        break;
                    case (int)StyleBase.ThemeNum.Light:
                        activeStyle = new LightStyle();
                        break;
                }
            }
            return activeStyle;
        }
        set { } }

2 个答案:

答案 0 :(得分:2)

看看这个blog post。特别是关于DynamicResourcesStyles的位。

<Application
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Your.App">
    <Application.Resources>
        <ResourceDictionary>
            <Color x:Key="backgroundColor">#33302E</Color>
            <Color x:Key="textColor">White</Color>
        </ResourceDictionary>
    </Application.Resources>
</Application>

现在设置你的资源

<Label Text="{Binding Name}" FontSize="Medium" FontAttributes = "Bold" TextColor = "{DynamicResource textColor}" LineBreakMode="NoWrap"/>
<Label Text="{Binding Text}" FontSize="Small" LineBreakMode="WordWrap" TextColor = "{DynamicResource textColor}"/>

现在,您可以在代码中随时更改资源

App.Current.Resources ["backgroundColor"] = Color.White;
App.Current.Resources ["textColor"] = Color.Black;

如果你使用的是2.3,你也可以试试built in themes

答案 1 :(得分:1)

您面临的问题是所有内容都已经呈现,并且您没有绑定任何属性,这些属性会将您在代码中所做的更改呈现给UI。您可以采用MVVM方法并创建属性并绑定到它们,并在UI线程中更改它们时进行通知。

如果您只对黑暗和浅色主题感兴趣,那么您可以使用内置的Light ThemeDark Theme。您还可以创建Custom Themes

  

通过包含主题,将一个主题添加到Xamarin.Forms应用程序中   Xamarin.Forms.Theme.Base Nuget包,加上一个额外的包   它定义了一个特定的主题(例如,Xamarin.Forms.Theme.Light)或者其他   可以为应用程序定义本地主题。

除了自动设置常用控件样式外,Light和Dark主题目前还支持以下类,可以通过在这些控件上设置StyleClass来应用这些类:

  • BoxView - 了Horizo​​ntalRule, 圈, 圆形

  • 图片 - 圈, 圆润, 缩略图

  • 按钮 - 默认, 主, 成功, 信息, 警告, 危险, 链接, 小, 大

  • 标签 - 头, 副标题, 身体, 链接, 逆

要在应用程序中添加主题,请执行以下操作:

  1. 将Nuget包添加到项目中。

  2. 在App.xaml

    中向主题词典添加主题

                      

  3. 使用Style类在主题中应用预定义的样式类。

    <Button Text="Button Class Default" StyleClass="Default" />
    <Button Text="Button Class Primary" StyleClass="Primary" />
    <Button Text="Button Class Success" StyleClass="Success" />
    
  4. 详细了解主题here

相关问题