2017-07-24 28 views
1

我想以此爲榜樣:如何在Xamarin中創建ResourceDictionary?

public class App : Application 
{ 
    public App() 
    { 
     var buttonStyle = new Style (typeof(Button)) { 
      Setters = { 
       ... 
       new Setter { Property = Button.TextColorProperty, Value = Color.Teal } 
      } 
     }; 

     Resources = new ResourceDictionary(); 
     Resources.Add ("buttonStyle", buttonStyle); 
     ... 
    } 
    ... 
} 

Global Styles

但它不工作/識別new ResourceDictionary()。有誰知道我可能會做錯什麼?

+0

定義'不起作用'。你有任何錯誤?如果是,他們是什麼? –

+0

將資源添加到資源字典後,您仍然必須在實際按鈕上設置樣式。例如,使用Xamarin.Forms;'

+0

''' – Milen

回答

1

您可以添加到您的二傳手款式的Setters集合。 PCL中的代碼:

 using Xamarin.Forms; 

     ..... 
     this.Resources = new ResourceDictionary(); 
     var style = new Style(typeof(Button)); 
     style.Setters.Add(new Setter { Property = Button.TextColorProperty, Value = Color.Teal }); 

     Resources.Add("MyButtonStyle", style); 
0

您需要實施資源字典。

創建像這樣一類:

public class MyResourceDictionary : ResourceDictionary 
{ 

} 

然後在你的代碼,你應該能夠做到:

Resources = new MyResourceDictionary(); 
Resources.Add("ButtonStyle",buttonStyle); 
1

您在資源文件中創建類

namespace MyNameSpace.Resources 
{ 
    public static class Styles 
    { 
     public static Style ButtonStyle = new Style(typeof(Button)) 
     { 
      Setters = { 
       new Setter { Property = Button.TextColorProperty, Value = Color.Teal } 

      } 
     }; 
    } 
} 

在你xaml.cs文件您可以直接使用,如

MyBtn.Style = Styles.ButtonStyle; 
1

應用程序應該已經有一個資源字典,特別是如果在XAML中定義的話。

檢查Resources是空

檢查Application.Current.Resources是空

如果兩者都爲空,你可以創建一個new ResourceDictionary(),您可能需要包括using Xamarin.Forms;

相關問題