我正在使用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; }
}
}
這裏是當應用程序啓動
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 { } }
你想在點擊事件之後改變主題嗎? – nishantvodoo
雅我想在那一刻改變它,然後在下次打開應用程序時保存該選擇..第二部分現在正在工作 – hartk1213
一旦已經呈現視圖,它將不會更新,除非你綁定到一個屬性。換句話說,您需要從頭開始重新渲染所有內容。 – nishantvodoo