0
votes

I'm developing a windows phone 8 application with default black background and white text color. I want to change these colors on each page of the application. How can I achieve this in the simplest manner?

I added the following style to my App.xaml file to see if it changes the color of all texts in a single page:

<Style x:Key="DefaultPage" TargetType="phone:PhoneApplicationPage">
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}"/>
        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="SupportedOrientations" Value="Portrait"/>
        <Setter Property="Orientation" Value="Portrait"/>
        <Setter Property="shell:SystemTray.IsVisible" Value="True"/>
    </Style>

But what it does is, it changes the color of all texts to red, except the ones that have pre-defined styles (like the title and the application name).

Any help or information on this would be highly appreciated.

3
You practically nailed it with this. There is no "simple absolute way" that i've ever heard of (I might be wrong, i'm just a rookie). Because whenever there is a "custom style" that defines the color and that style is applied on one of the containers closer to the object then your general style, it overrides it. So one partial solution might be to override system resources used by the "generic windows phone styles" and combine it with styles based on the ones originally used. (basically combining the two answers you already got) + using your current solutionmishan

3 Answers

0
votes

Don't have Visual Studio at the Moment. I would suggest to override the System resources. something like that:

((SolidColorBrush)Resources["PhoneBackgroundBrush"]).Color = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);
((SolidColorBrush)Resources["PhoneTextBoxBrush"]).Color = Color.FromArgb(0xBF, 0xFF, 0xFF, 0xFF);
((SolidColorBrush)Resources["PhoneTextBoxForegroundBrush"]).Color = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);
0
votes

If you have an app title defined like this:

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="APP NAME" 
                       Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Page Title" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

The font color defined in page style will be overriten by PhoneTextNormalStyle. So you need to create a custom style for it:

<Style x:Key="RedPhoneTextNormalStyle" TargetType="{x:Type TextBlock}" 
                              BasedOn="{StaticResource PhoneTextNormalStyle}">
        <Setter Property="Foreground" Value="Red"/>
</Style>

And then you should apply the new style to your TextBlock

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" 
                       Text="APP NAME" 
                       Style="{StaticResource RedPhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Page Title" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
0
votes

The simplest way of doing it is by using Foreground property of TextBlock and TextBox elements. All you have to do is give a name to the element, if it is a predefined one(in the XAML, that its) and Visual Studio will automatically create an object for it. Let's say the name of the element is textBlock in your XAML file. All you have to do is write the line:

textBlock.Foreground = new System.Drawing.SolidBrush(System.Drawing.Color.Red);

, if you want to use a predefined brush or

textBlock.Foreground = (Brush) (new System.Windows.Media.BrushConverter()).ConvertFromString("#FF0000")

, if you want to use a brush color of a hexadecimal code of your choice. That should help you color the text you want in the color you want.

Additionally, you can define a <SolidColorBrush Color="#FF0000" x:Name="Red"/> inside the <Page.Resources></Page.Resources> like this:

<Page.Resources> <SolidColorBrush Color="#FF0000" x:Name="Red" /> </Page.Resources>

and then use the SolidColorBrush wherever you want by using something as simple as: textBlock.Text = Red, anywhere in the code.

Hope that helped.