get phone accent brush programmatically c#

2019-03-18 08:56发布

问题:

I have textbox in xaml

<TextBlock Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeLarge}" FontFamily="{StaticResource PhoneFontFamilySemiLight}" Margin="12,10,12,0" />

How can I get value of phoneaccentbrush, programmatically (c#) from system resource of windows phone 7 / 7.5 / 8 so that i can set the foreground-color to match the accent selected in the WP's settings.

回答1:

First, you need to create currentAccentColorHex before Constructor of you C# class:

public partial class MainPage : PhoneApplicationPage
{
    Color currentAccentColorHex = (Color)Application.Current.Resources["PhoneAccentColor"];

    // Constructor
    public MainPage()
    {          
        //...

and then use it wherever you need to set color for the control: Example for Background property for control MyControl:

SolidColorBrush backColor = new SolidColorBrush(currentAccentColorHex);
MyControl.Background = backColor;

Hope this help



回答2:

thanks Spaso :) I did little more research and with your help I came up with following code

var phoneAccentBrush =  new SolidColorBrush((App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush).Color);


回答3:

add this to your textbox at xaml

Foreground="{StaticResource PhoneAccentBrush}"

or set this from c#

btnDefault.Foreground = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]);