get phone accent brush programmatically c#

我们两清 提交于 2019-12-20 18:04:30

问题


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"]);


来源:https://stackoverflow.com/questions/14039981/get-phone-accent-brush-programmatically-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!