DataTemplate not found in merged ResourceDictionary - xaml

I'm currently developing a universal app for Windows Phone 8.1 and Windows 8.1.
I share most of my code, but I'm willing to keep style resources appart.
Some context : First, right now I've only started the WP8.1 projects so everything is related to this platform. In this WP8.1 project, I have a MainPage.xaml which contains a Pivot control. One of the PivotItem is a UserControl called, for clarity purposes, MyUserControl.
I created a resource dictionary Styles.xaml into my "Assets" directory, in BOTH platform projects. Then, I registered those 2 new files into my shared App.xaml like this :
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<ResourceDictionary x:Key="ResourceDictionary">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Assets/Styles.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
In the WP8.1 project Styles.xaml file, I created a DataTemplate :
<DataTemplate x:Key="MyDataTemplate">
<TextBlock Text="This is a textblock" />
</DataTemplate>
In MyUserControl, I've added a ListView and bound it on the ItemTemplate property to MyDataTemplate like this :
<ListView ItemTemplate="{StaticResource MyDataTemplate}" ItemsSource="{Binding MyContent}" />
When I run the solution and I end up with this error :
Cannot find a Resource with the Name/Key "MyDataTemplate"
Does anyone know why I encounter this error ? What did I do wrong ?
An odd thing : when I right click > "Go To Definition" on the MyDataTemplate bound in the ListView.ItemTemplate property, it routes me to the right place.
Thank you in advance for your help !

Your code is wrong, here is the correct one
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Assets/Styles.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>

Related

How to reference a dictionary in another project WPF

[Custom Control Wpf Library]
Test
-->Resources
-->Resources
-->Dictionary1.xaml (Reusable across multiple project)
-->Dictionary2.xaml(Reusable across multiple project)
[WPF main UI] TestRD
-->Resources
-->Resources
-->Dictionary9.xaml(Specific to the project)
XAML:
Dictionary1.xml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="brush" Color="Red"/>
</ResourceDictionary>
Dictionary2.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="brush" Color="Green"/>
</ResourceDictionary>
Dictionary9.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--How can I use the resource from another DLL here-->
</ResourceDictionary>
How can I reference Dictionnary1 or dictionary2 in Dictionary9?
Is it advisable, especially in a modular project?
I have some styling I would like to move globally so I can reuse and stop copy and pasting them.
How can I reference Dictionnary1 or dictionary2 in Dictionary9?
Try this:
Dictionary9.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--How can I use the resource from another DLL here-->
<ResourceDictionary Source="pack://application:,,,/Resources;component/Resources/Dictionary1.xaml" />
<ResourceDictionary Source="pack://application:,,,/Resources;component/Resources/Dictionary2.xaml" />
</ResourceDictionary>
Note that the 1st "Resources" is your lib name and the 2nd "Resources" is the folder name inside your lib.
You can find more info here.
Is it advisable, especially in a modular project?
IMO, is totally OK if you're reusing or need to separate these dictionaries.

Not able to access StaticResource in Xamarin Forms from app.xaml

I am not able to access static resources that created in app.xaml.
its my app.xaml code
<Application.Resources>
<ResourceDictionary>
<Color x:Key="AppDefaultColor">#07bab7</Color>
<Color x:Key="PageDefaultColor">#f0f0f0</Color>
</ResourceDictionary>
</Application.Resources>
when i try to use this in my page as static resource it gives error.
its my page code which i used to access static resource from app.xaml
<Label Text="Test" TextColor="{StaticResource AppDefaultColor}" />
Please Check that your app.xaml.cs class call InitializeComponent() method. In the App constructor which is required when creating the App.xaml and associated code behind.
Please look into picture

Override theme resource in the scope of a Page

I want to override a theme resource, specifically the SystemAccentColor, in the scope of a specific page.
I have successfully done it in application wide scope. But I can't do it for a specific page.
XAML in App.xaml (this works fine)
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<Color x:Key="SystemAccentColor">#862E2D</Color>
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="Black"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
XAML in Page.xaml (this doesn't work)
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<Color x:Key="SystemAccentColor">#111111</Color>
<SolidColorBrush x:Key="SystemControlHighlightAccentBrush" Color="Black"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Page.Resources>
<!-- Control to test that the resource is overriden. The `ProgressBar` uses the accent color by default -->
<ProgressBar
Height="10"
Grid.ColumnSpan="3"
VerticalAlignment="Top"
IsIndeterminate="True" />
I don't know why the resource is not overridden in the scope of the page.
I have tried removing the override from App.xaml and only overriding the resources in Page.xaml but that doesn't work either.
However if I do this
XAML in Page.xaml
<ProgressBar
Height="10"
Grid.ColumnSpan="3"
VerticalAlignment="Top"
IsIndeterminate="True"
Foreground="{StaticResource SystemControlHighlightAccentBrush}"/>
Then the ProgressBar gets the correct foreground value. Which means that the ovverride does take place.
Does anybody know why this is happening? Or how I can override a theme resource for a specific page?
The best way is using Visual Studio or 'Blend for Visual Studio'. I'm using Visual Studio
Right click on the Element you want to edit (in your case, Progress bar) > select 'Edit Template' > 'Edit a Copy'
A 'Create Style Resource' shows up, Enter a name for your style & in the 'Define in' section, select 'This Document' (this ensures the style only applies to the page/window and not app wide) and click okay
Immediately, the Progress Bar includes a style attribute (see last image) using the new style you created. You can go ahead and modify the code snippet inserted by the IDE to suite your imagination.
I hope this helps out.

Retrieve a value from ResourceDictionary in code behind

I have a Windows 10 universal app with a few resource dictionaries for styles and such.
In one such resource dictionary, I have a color, MyBlue that I want to access via the code behind. How is this achieved?
I've tried this.Resources["MyBlue"], but since MyBlue is defined in a separate resource dictionary and not directly in the page resources it doesn't exist in this collection.
Here's my app.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/PodStyles.xaml"/>
<ResourceDictionary Source="Styles/ButtonStyles.xaml"/>
<ResourceDictionary Source="Styles/OfferStyles.xaml"/>
<ResourceDictionary Source="Styles/MyStyles.xaml"/>
<ResourceDictionary>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Have you tried
Application.Current.Resources["MyBlue"]
MergedDictionaries are available via Application.Current.Resources.MergedDictionaries.
I was able to obtain the appropriate ResourceDictionary by Uri and then access the item in it by key name:
var mergedDict = Application.Current.Resources.MergedDictionaries.Where(md => md.Source.AbsoluteUri == "ms-resource:///Files/Styles/MyStyles.xaml").FirstOrDefault();
newColor = (Windows.UI.Color)mergedDict["MyBlue"];
I will try to sum-up all the solution as all previous solution seems to have part of the whole answer.
So there's two main cases :
Case A. Your value is in the main ResourceDictionary of your App.xaml
Where App.xaml is :
...
<Application.Resources>
<ResourceDictionary>
<Color x:Key="backgroundColor">LightBlue</Color>
</ResourceDictionary>
</Application.Resources>
...
If so read your value by using :
Application.Current.Resources["backgroundColor"]
Case B. Your value is in an external ResourceDictionary merged in your App.xaml
Where App.xamlis :
...
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Constants.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
...
Where Constants.xaml is :
<?xml version="1.0" encoding="utf-8" ?>
<?xaml-comp compile="true" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Color x:Key="backgroundColor">LightBlue</Color>
</ResourceDictionary>
In this case use #earthling solution :
var mergedDict = Application.Current.Resources.MergedDictionaries.Where(md => md.Source.OriginalString.Equals("Styles/Constants.xaml")).FirstOrDefault();
var color = (Color)mergedDict["pressedColor"];
The ResourceDictionary you want to reference has to be pulled into the application resources somehow. Either merge it into your Application's resources (if it's common enough to be included all the time, at a slight hit to your app's initialization), or merge it into your Page's resources (at a slight hit to the first page initialization).
You include foreign ResourceDictionaries with this syntax:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="path/to/resource.xaml" />
</ResourceDictionary>
<... your other resources for this page go here .../>
</ResourceDictionary>
</Page.Resources>

Using a ResourceDicitonary in a Windows Phone 8.1 Portable Class Library

I have a Windows Phone 8.1 Portable Class Library with a ResourceDictionary in Themes/Generic.xaml
Now, I want to declare styles there and use them in other Controls I have, in my ResourceDictionary I have something like:
<Style x:Key="myCustomStyle" TargetType="ProgressBar">
{...}
</Style>
And in my Control, something like:
<Page.Resources>
<ResourceDictionary x:Key="dictionary">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyProject;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
{...}
<ProgressBar IsIndeterminate="True" Style="{StaticResource myCustomStyle}" />
But this does not work, but if I have the Style in my Control, it works fine, it's the ResourceDictionary reference that's not working.
How can I make it work?