Why does overriding ApplicationPageBackgroundThemeBrush not work? - xaml

I am trying to define a default background colour for my Windows 8 Store application, but although it shows correctly in the XAML editor and in Blend, it comes up with the default black background when run on Windows 8 and in the Windows RT emulator.
I created a brand new Windows 8 app based on the "Split App" VS 2012 template and modified App.xaml to specify a new value for ApplicationPageBackgroundThemeBrush.
This is what my App.xaml looks like:
<Application
x:Class="App3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App3"
xmlns:localData="using:App3.Data">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Application-specific resources -->
<x:String x:Key="AppName">App3</x:String>
<!-- Basic foreground and background colours -->
<SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="#FF3CA5DC"/>
<SolidColorBrush x:Key="ApplicationPageForegroundThemeBrush" Color="White"/>
</ResourceDictionary>
</Application.Resources>

It seems that those 2 brushes are only used by a few styles in StandardStyles.xaml, one of them is
<Style x:Key="LayoutRootStyle" TargetType="Panel">
which you can apply to your root panel. But your changes in the App.xaml doesn't affect this style. It affects only further use of this brush, so if you want to use those particular brushes I see the following variants:
1) Declare them in your App.xml and use it further like:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
2) Declare them in StadardStyles.xaml under
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<!-- Style Goes Here -->
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
In this case all StandardStyles will be affected, but you should use LayoutRootStyle in your Grid also.
But really, using those brushes show so litle profit that I think it is better just set your panel background to what you need.

Related

Can we define resource for xamarin forms control template elsewhere other than app.xaml

I am trying to build pages with a base template. I've used control template in app.xaml and it works great.
Here is what I have done
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App3.App">
<Application.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="baseTemplate">
But can I define this control template resource anywhere other than app.xaml because the app.xaml is getting cramped. Can I have separate folder for templating. How can I achieve this?
Any guide or tutorial will also help.
You can define a ResourceDictionary in any XAML file. See the Xamarin documentation here:
There's also a good Xamarin University class on this:
https://university.xamarin.com/classes/track/xamarin-forms#xam140-resources-and-styles
If you're new to Xamarin, I suggest you hit Xamarin University. The self-guided courses are free.

How can I change a style via code that is stored in a resource dictionary in App.xaml on the fly?

I am trying to have the ability for several core colors throughout the app to be changeable on the fly by the user. My current idea is to have a single SolidColorBrush that is my "theme color" and then to reference that throughout my main styles.
<SolidColorBrush x:Key="ReflectiveColor1" Color="#FF0E777B"></SolidColorBrush>
This snippet is stored in a resourcedictionary that is reference in my App.xaml
<Application.Resources>
<ResourceDictionary Source="DictionaryAlienwareTheme.xaml">
</ResourceDictionary>
</Application.Resources>
When I try and access the color in my App.cs I get an error
Application.Current.Resources["ReflectiveColor1"] = Colors.Black;
Error:
System.Runtime.InteropServices.COMException: 'No installed components were
detected.
Local values are not allowed in resource dictionary with Source set
Is there anyway to make this work? I'm assuming this error is because it doesn't want us to modify styles stored there, but I don't know the workaround.
Firstly, in your App.xaml if you want to reference the DictionaryAlienwareTheme.xaml you defined by yourself you need to merge the dictionaries to current app as #AVK said. More details please reference "Merged resource dictionaries" section of this article.
Secondly, after you use the merged resource, if you want to update the theme resource code behind you may need to use Application.Current.Resources.MergedDictionaries to visit the merged dictionaries instead of Application.Current.Resources. For example,
public MainPage()
{
this.InitializeComponent();
var mergedDict = Application.Current.Resources.MergedDictionaries.FirstOrDefault();
((SolidColorBrush)mergedDict["ReflectiveColor1"]).Color = Colors.Red;
}
By the way, as above code snippet showed, ReflectiveColor1 is SolidColorBrush you cannot set color directly.
Application will not be able to find the Resource ReflectiveColor1 until you merge all resource dictionaries together.
Change your App.xaml to Merge the dictionaries. Like below.
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DictionaryAlienwareTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Then you should be able to access the Resource in the same way that you were doing before.

How to change the default style of a XAML control?

I want to change the default style or theme of the pivot control and I've found some really cool samples online, but i don't know where to place them in.
should i make a new file or edit an existing one?
You can create a new XAML ResourceDictionary in the project, where you can place these styles.
After that you have to include this resource dictionary as merged dictionary in the App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///MyFolder/MyResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Of course you don't have to create seperate file for these resources, you can place these to any Resources collection. Like to the Application.Resources in App.xaml or to the Page.Resourcesin the page's xaml.
A style will be default to the target control, if you don't provide any x:Key="" to them in the resource collection. These are implicit styles.

WP8 Change pivot header color (selected / unselected)

I need to change a pivot header color but still have a different color for selected and unselected pivot item headers.
I can easily change the color of all the headers but i really need a way to differentiate selected and unselected pivot items.
I have tried many ways to do this for windows phone 7 but the pivot header architecture seem to have changed and the styles no longer work.
I couldn't find a direct way of doing this by for example creating a new headertemplate, but you change this applicationwide by overriding the correct solidcolorbrushes in the theme dictionary. So in App.xaml:
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush
x:Key="PivotHeaderForegroundUnselectedBrush"
Color="Purple"/>
<SolidColorBrush
x:Key="PivotHeaderForegroundSelectedBrush"
Color="Orange"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
This is not a completely foolproof approach, it is explained in more detail at http://msdn.microsoft.com/library/windows/apps/br208807

Silverlight ResourceDictionary not available

I've put a few good hours into this.. I'm unable to see the various styles I have defined in a global Resource dictionary. The external file is called Styles.xaml. What am I missing?
There are a few things to consider using MergedDictionaries.
First you should set the BuildAction of the ResourceDictionary (Styles.xaml) to Resource or Content (I tried it with Resource).
Second in your App.xaml do not define a Key for the ResourceDictionary (remove x:Key="ButtonStyles")
Then put all other resources in your Application.Resources in the ResourceDictionary.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Assets/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<CCE_2009_Client_ViewModels:ViewModelLocator x:Key=ViewModelLocatorDataSource/>
<!-- Any additional resources -->
</ResourceDictionary>
</Application.Resources>
After all this is done then you should select the Style in the Properties Window.
Do you have referenced CCE_2009.Client assembly (project) in the project you want to use this style?
If not this can be a fix for your problem.