2017-04-18 46 views
1

在App.xaml中我有這樣的代碼:如何從Xamarin Forms中的C#中的App.xaml訪問顏色?

<Application.Resources> 
    <ResourceDictionary> 
     <Color x:Key="Yellow">#ffd966</Color> 
    </ResourceDictionary> 
</Application.Resources> 

,並在C#我有這樣的代碼:

public Color BackgroundColor 
{ 
    get { return IsSelected ? Color.Yellow : Color.White; } 
} 

而且我想改變Color.Yellow從App.xaml中的顏色。我如何從C#中的App.xaml中引用顏色?

+0

isSelected? (顏色)Application.Current.Resources [「Yellow」]:Color.White; – Dilmah

+0

你可以發表一個答案,我會接受它 – Uros

回答

5
isSelected ? (Color) Application.Current.Resources["Yellow"] : Color.White; 

我覺得不需要轉換Color.FromHex()如要定義資源的顏色。希望有所幫助。

+0

啊,你是對的! –

1

您應該可以像這樣訪問Application.Current.Resources["Yellow"]

在顏色的情況下,它會更像;

public Color BackgroundColor 
{ 
    get { return IsSelected ? Application.Current.Resources["Yellow"].ToString() : Color.White } 
} 
相關問題