2016-08-18 181 views
2

我正在使用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 { } } 
+0

你想在點擊事件之後改變主題嗎? – nishantvodoo

+0

雅我想在那一刻改變它,然後在下次打開應用程序時保存該選擇..第二部分現在正在工作 – hartk1213

+0

一旦已經呈現視圖,它將不會更新,除非你綁定到一個屬性。換句話說,您需要從頭開始重新渲染所有內容。 – nishantvodoo

回答

2

看一看這個blog post加載的配色方案App.cs文件的一部分。特別是關於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}"/> 

現在的代碼,你可以如果你使用2.3的飛行

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

改變你的資源,你也可以嘗試built in themes

+0

感謝你。我現在可以在運行時更改窗體顏色。 – fuzz

1

你面臨的問題是,一切都已經呈現,你沒有約束t o任何將重新呈現您在代碼中對UI所做更改的屬性。您可以採用MVVM方法並創建屬性並綁定到它們,並在UI線程中更改時通知它們。

如果您只對黑暗和光明主題感興趣,那麼您可以使用內置的Light ThemeDark Theme。您也可以創建Custom Themes

一個主題是由包括 Xamarin.Forms.Theme.Base NuGet包,再加上一個附加的包 ,其限定特定主題(例如,添加到Xamarin.Forms應用。Xamarin.Forms.Theme.Light )或者其他 可以爲應用程序定義本地主題。

除了自動造型共同控制的光明和黑暗的主題目前支持可應用於以下類通過對這些控制設置的styleClass:

  • BoxView中 - 了Horizo​​ntalRule, 圈, 四捨五入

  • 圖像 - 圈, 圓角, 縮圖

  • 按鈕 - 默認, 小學, 成功, 信息, 警告, 危險, 鏈接, 小, 大

  • 標籤 - 頭, 副標題, 身體, Link, Inverse

一個主題添加到您的應用程序,請執行以下操作:

  1. 添加的NuGet包到項目中。

  2. 到資源字典中添加主題的App.xaml

  3. 使用樣式類主題,以應用預定義的樣式類。

    <Button Text="Button Class Default" StyleClass="Default" /> 
    <Button Text="Button Class Primary" StyleClass="Primary" /> 
    <Button Text="Button Class Success" StyleClass="Success" /> 
    

瞭解更多關於主題here