Use resource file in XAML file (Xamarin) - xaml

How to use resource files in XAML (xamarin forms) like <Label Text="MyApp.resouces.MyString" />?

There is a nice article here covering this topic. It includes lot of examples.
After you implement your TranslateExtension your code will look like this:
<Label x:Name="lblName" Text="{local:TranslateExtension MyString}" />

Your App.axml file:
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Master.App">
<Application.Resources>
<!-- Application resource dictionary -->
<ResourceDictionary>
<x:String x:Key="AppName">Name of app</x:String>
</ResourceDictionary>
</Application.Resources>
</Application>
And then your label:
<Label Text="{StaticResource AppName}"/>

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

List<Color> in Xaml

I have my List defined in Xaml like this.
<ContentPage.Resources>
<ResourceDictionary>
<local:FileName x:Key="fileName">
<Color>#3599B8</Color>
<Color>#374649</Color>
<Color>#FD625E</Color>
<Color>#F2C80F</Color>
</local:FileName>
</ResourceDictionary>
</ContentPage.Resources>
FileName is defined in code behind like this.
public class FileName : List<Color>
{
}
Instead of directly setting the Color values, i want to define it as resource like this
<Color x:Key="BasicColorSchemeBlue">#3599B8</Color>
and use it.
Any suggestions on how to do this.
Thanks in advance.
GradientColors is an array of Colors.
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:GradientTest"
xmlns:s="clr-namespace:System;assembly=mscorlib"
x:Class="GradientTest.GradientTestPage">
<StackLayout Padding="20, 40, 20, 20">
<local:GradientViewRender HorizontalOptions="Center"
WidthRequest="300"
HeightRequest="50"
x:Name="gradientView">
<local:GradientViewRender.GradientColors>
<x:Array Type="{x:Type Color}">
<Color>#5FC900</Color>
<Color>#0FF2C8</Color>
</x:Array>
</local:GradientViewRender.GradientColors>
</local:GradientViewRender>
</StackLayout>
</ContentPage>
I have done like this on a working App.
This goes inside App.xaml:
<Color x:Key="COLOR_NAME">#ffffff</Color>
to access the color from a .cs file, use:
(Color)ResourceFinder.FindResource("COLOR_NAME");
Or use StaticResource or DynamicResource within an xaml file.

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>

DataTemplate not found in merged ResourceDictionary

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>