I'm trying to override some default SolidColorBrush for my whole app:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="ListViewItemBackgroundSelected" Color="{ThemeResource SystemAccentColorLight1}"/>
<SolidColorBrush x:Key="ListViewItemBackgroundSelectedPointerOver" Color="{ThemeResource SystemAccentColorLight2}"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="ListViewItemBackgroundSelected" Color="{ThemeResource SystemAccentColorDark1}"/>
<SolidColorBrush x:Key="ListViewItemBackgroundSelectedPointerOver" Color="{ThemeResource SystemAccentColorDark2}"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
<ResourceDictionary>
</Application.Resources>
When I add these specific SolidColorBrush to my ThemeDictionary in App.xaml (I have more resources placed there) it has no effect.
However, when I place this ThemeDictionary in the Application.Resources of a Page with a ListView it does have an effect.
What is the problem here, and how can I solve it?
Related
I'm creating a UWP library that contains custom controls and I want to define a dark and light theme for my controls. I understand that we can add a theme resource dictionary to the App.xaml. We can specify a dark/light theme there and set the requested theme property. But since I am making a library, I don't have an App.xaml in my project. Is it possbile for me to define a theme within the library project? How?
You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Then we can define the ThemeDictionaries in the ResourceDictionary file.
For example:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ClassLibrary1">
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="CustomColor" Color="Orange" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="CustomColor" Color="Blue" />
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<SolidColorBrush x:Key="CustomColor" Color="Green" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
To use that dictionary, we can merge it with control’s dictionary:
<UserControl
x:Class="ClassLibrary1.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ClassLibrary1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Rectangle Fill="{ThemeResource CustomColor}" Width="500" Height="500"></Rectangle>
</Grid>
</UserControl>
If you want to specify a dark/light theme, you can set RequestedTheme in the Control.
I want to paint Stack Panel surface using Acrylic brush.
<StackPanel Background="{ThemeResource SystemControlAcrylicElementBrush}"></StackPanel>
It works for me but there is a problem when I want to change Tint color and opacity. There is a following code to change it:
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<AcrylicBrush x:Key="MyAcrylicBrush"
BackgroundSource="HostBackdrop"
TintColor="#FFFF0000"
TintOpacity="0.8"
FallbackColor="#FF7F0000"/>
</ResourceDictionary>
I don't know where should I place it and rename brush for this?
<StackPanel Background="{ThemeResource **MyAcrylicBrush**}"></StackPanel>
Thanks for help.
P.S. You need Windows Insider SDK and system build 16190 or higher
You can create a ResourceDictionary, for example called "ThemeDictionary.xaml" and put the code you have for your AcrylicBrush in there.
Then in your App.xaml you can reference your ResourceDictionary like so:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ThemesDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Method 1:
Add ResourceDictionary.ThemeDictionaries in Application.Resources
In App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<AcrylicBrush x:Key="MyAcrylicBrush" BackgroundSource="HostBackdrop" TintColor="#FFFF0000" TintOpacity="0.8" FallbackColor="#FF7F0000"/>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<SolidColorBrush x:Key="MyAcrylicBrush" Color="{ThemeResource SystemColorWindowColor}"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<AcrylicBrush x:Key="MyAcrylicBrush" BackgroundSource="HostBackdrop" TintColor="#FFFF0000" TintOpacity="0.8" FallbackColor="#FFFF7F7F"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
Method 2:
As #jsmyth886 answered
Add a separate ResourceDictionary file and place your ResourceDictionary.ThemeDictionaries code
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<AcrylicBrush x:Key="MyAcrylicBrush" BackgroundSource="HostBackdrop" TintColor="#FFFF0000" TintOpacity="0.8" FallbackColor="#FF7F0000"/>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<SolidColorBrush x:Key="MyAcrylicBrush" Color="{ThemeResource SystemColorWindowColor}"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<AcrylicBrush x:Key="MyAcrylicBrush" BackgroundSource="HostBackdrop" TintColor="#FFFF0000" TintOpacity="0.8" FallbackColor="#FFFF7F7F"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
You MergedDictionaries in App.xaml to Merged your ResourceDictionary file
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
For more Info: ResourceDictionary and XAML resource references, XAML for Windows 10 Controls - Styling
If I need to override a color in a brush, I would set it into App.xaml like this:
<Application.Resources>
...
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="PivotHeaderForegroundUnselectedBrush" Color="#A7A9AC" />
<SolidColorBrush x:Key="PivotHeaderForegroundSelectedBrush" Color="Black" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
...
</Application.Resources>
The editor underlines <ResourceDictionary> saying: "Each dictionary entry must have an associated key".
How can solve this?
I solved the problem, thanks to #Alan Yao - MSFT.
Maybe you have more things there?, I had something else, written into the ResourceDictionary. By making ResourceDictionary parent of everything inside Application.Resources, I solved the problem.
The ResourceDictionary has to be placed inside application resources:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="PivotHeaderForegroundUnselectedBrush" Color="#A7A9AC" />
<SolidColorBrush x:Key="PivotHeaderForegroundSelectedBrush" Color="Black" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
i want to change the accent color in my app (windows universal app) from the default one to a custom color that is defined in my app.
Any idea how can I do this?
You cannot really change the base Accent color in your application because that's defined by the system theme color (Taskbar, Startmenu, etc...). However, you can MODIFY the Accent color by using styles. Here's an example:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1">
<!-- Basic Colors -->
<SolidColorBrush x:Key="SystemAccentTransparentHighBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.9"/>
<SolidColorBrush x:Key="SystemAccentTransparentMediumHighBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.7" />
<SolidColorBrush x:Key="SystemAccentTransparentMediumBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.6" />
<SolidColorBrush x:Key="SystemAccentTransparentMediumLowBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.5" />
<SolidColorBrush x:Key="SystemAccentTransparentLowBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.3" />
<SolidColorBrush x:Key="SystemAccentHighlightBrush" Color="{ThemeResource SystemAccentColor}" />
<SolidColorBrush x:Key="SystemBlackBrush" Color="{ThemeResource SystemChromeBlackHighColor}" />
<SolidColorBrush x:Key="SystemWhiteBrush" Color="{ThemeResource SystemChromeWhiteColor}" />
<SolidColorBrush x:Key="SystemGreyHighBrush" Color="#FFE0E0E0" />
<SolidColorBrush x:Key="SystemGreyMediumHighBrush" Color="#FFA0A0A0" />
<SolidColorBrush x:Key="SystemGreyMediumBrush" Color="#FF808080" />
<SolidColorBrush x:Key="SystemGreyMediumLowBrush" Color="#FF606060" />
<SolidColorBrush x:Key="SystemGreyLowBrush" Color="#FF404040" />
<SolidColorBrush x:Key="SystemAppBackgroundBrush" Color="{ThemeResource ApplicationPageBackgroundThemeBrush}" />
<Style x:Key="GridStyle_Page" TargetType="Grid">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="Background" Value="{StaticResource SystemBlackBrush}" />
</Style>
</ResourceDictionary>
You would put that in a styles.xaml file. Then you have to tell the system about it as follows in the App.xaml file:
<Application
x:Class="App1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Finally, you use the styles/colors in your application by doing one of the following:
Add a Style="{StaticResource }" to the element that you
want to style. You would replace with a style that is
defined in the Styles.xaml file above.
Add a color attribute such as Forground, Background, or Border to
the element that you want to color. To do this, add something like
Background="{StaticResource }" where the is
replaced by one of the color styles above.
To modify the accent color, you need to overlay it over a background color. Notice in the SolidColorBrush definitions above. Some of them have an Opacity setting. The range for this setting is between 1.0 and 0.0 where 1.0 is completely opaque while 0.0 is completely transparent. So the lower the Opacity setting, the more of the underlying color shows through. If the underlying color is black, then a lower Opacity setting will give you a darker Accent color. If the underlying color is white, then you will get a lighter Accent color as the Opacity value decreases.
A note about styles: Much of how xaml works is through styles. There are a couple of files on the computer that defines what all the styles are in the system. Those files are generic.xaml and themeresources.xaml. On my system (Windows 10 Pro RTM with VS2015), these files are found in the following location:
C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10240.0\Generic
I hope this helps and sorry for the late answer. I just came across your question while I was googling for something else.
Have you tried using:
(App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush).Color = Colors.Blue;
in you App.xaml.cs?
I am trying to define a standard foreground color for the controls in my page. However, I get the error "An object of the type "System.String" cannot be applied to a property that expects the type "Windows.UI.Xaml.Media.Brush".
In myPage.xaml
<TextBlock TextWrapping="Wrap"
Foreground="{StaticResource ForegroundThemeBrush}" />
In StandardStyles.xaml
<ResourceDictionary x:Key="Default">
<x:String x:Key="BackgroundThemeBrush">#484848</x:String>
<x:String x:Key="ForegroundThemeBrush">#efefef</x:String>
</ResourceDictionary>
you need to define SolidColorBrush not x:String
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="BackgroundThemeBrush" Color="#484848"/>
<SolidColorBrush x:Key="ForegroundThemeBrush" Color="#efefef"/>
</ResourceDictionary>
You should use a SolidColorBrush instead of a x:String for your brush.