在运行时更改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; } } } 

这里是上面文件inheritance的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 { } } 

看看这篇博客文章 。 特别是关于DynamicResourcesStyles

    #33302E White    

现在设置你的资源

   

现在,在代码中,您可以动态更改资源

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

如果你使用2.3,你也可以尝试内置主题

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

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

通过包含Xamarin.Forms.Theme.Base Nuget包以及定义特定主题的附加包(例如Xamarin.Forms.Theme.Light)或者本地主题可以添加到Xamarin.Forms应用程序的主题为应用程序定义。

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

  • BoxView – Horizo​​ntalRule,Circle,Rounded

  • 图像 – 圆形,圆形,缩略图

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

  • 标签 – 标题,副标题,正文,链接,反向

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

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

  2. 在App.xaml中将主题添加到资源字典

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

        

点击此处了解更多主题。