I have defined a style , but I'd like to set the Android and iOS values to a DynamicResource - What's the syntax?
<Style
TargetType="SearchBar" ApplyToDerivedTypes="true">
<Setter Property="BackgroundColor" Value="{OnPlatform Android='White', iOS='#4F8BBF'}"/>
</Style>
Per official documentation for different values for different targets
<!-- Colors -->
<Color x:Key="AppBackgroundColor">WhiteSmoke</Color>
<Color x:Key="iOSNavigationBarColor">WhiteSmoke</Color>
<Color x:Key="AndroidNavigationBarColor">#2196F3</Color>
<Color x:Key="iOSNavigationBarTextColor">Black</Color>
<Color x:Key="AndroidNavigationBarTextColor">White</Color>
<!-- Implicit styles -->
<Style TargetType="{x:Type NavigationPage}">
<Setter Property="BarBackgroundColor"
Value="{OnPlatform iOS={StaticResource iOSNavigationBarColor},
Android={StaticResource AndroidNavigationBarColor}}" />
<Setter Property="BarTextColor"
Value="{OnPlatform iOS={StaticResource iOSNavigationBarTextColor},
Android={StaticResource AndroidNavigationBarTextColor}}" />
</Style>
<Style TargetType="{x:Type ContentPage}" ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor" Value="{StaticResource AppBackgroundColor}" />
</Style>
That's for setting different values
But if you are really looking for a literal dynamic resource, you'll have to set it in code behind constructor.
Here is the example
Resources ["searchBarStyle"] = Resources ["blueSearchBarStyle"];
Related
I'm trying to avoid duplicate declaration of style definitions in both android styles.xml and resourcedictionary. For example,
In Android styles.xml, I've the following to change the default theme's primary color
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="MyDefaultTheme">
<item name="colorPrimary">#color/colorPrimary</item>
</style>
<color name="colorPrimary">#0E80FF</color>
</resources>
And in DefaultResourceDictionary.xaml, I've the following so that my XF's controls uses the same color as defined in styles.xml:
<ResourceDictionary ...>
<Color x:Key="colorPrimary">#0E80FF</Color>
</ResourceDictionary>
On top of my head, I can think of doing some PreBuild actions to invoke XmlPoke to insert the value from ResourceDictionary into styles.xml.
Is there any less complicated ways of doing it?
Yes you can add styles in the App Class.
Example adding this in the App.xaml file
<Application.Resources>
<ResourceDictionary>
<Color x:Key="PrimaryColor">#512bdf</Color>
<Color x:Key="SecondaryColor">White</Color>
<Style TargetType="Label">
<Setter Property="TextColor" Value="{DynamicResource PrimaryColor}" />
<Setter Property="FontFamily" Value="OpenSansRegular" />
</Style>
<Style TargetType="Button">
<Setter Property="TextColor" Value="{DynamicResource SecondaryColor}" />
<Setter Property="FontFamily" Value="OpenSansRegular" />
<Setter Property="BackgroundColor" Value="{DynamicResource PrimaryColor}" />
<Setter Property="Padding" Value="14,10" />
</Style>
</ResourceDictionary>
</Application.Resources>
How to override default blue titlebar color of Xamarin.Forms UWP app through AppShell style or something?
Color Problem
If you want to change the Blue color of the bar of your sceenshot in shell app,you can set the value of property Shell.BackgroundColor:
<Shell.Resources>
<ResourceDictionary>
<Style x:Key="BaseStyle" TargetType="Element">
<!--change the color here-->
<Setter Property="Shell.BackgroundColor" Value="#96F3" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
I have this code:
<ResourceDictionary>
<Color x:Key="WordTextColor">#2196f3</Color>
</ResourceDictionary>
<Grid>
<Grid.Resources>
<Style TargetType="Grid">
<Style TargetType="Label">
<Setter Property="TextColor" Value="{StaticResource WordTextColor}" />
</Style>
How can I code this in C#
Also can I code up this Static Resource as a string instead of a Color?
You can have a helper class, like this, where you define all your colors / values
public static class Styles
{
private static Color _backgroundColor = Color.FromHex("151515");
public static Color BackgroundColor => _backgroundColor;
}
Then, in xaml, you reference it in the header:
xmlns:local="clr-namespace:YourProjectAssembly.YourName;assembly=YourProjectAssembly.YourName"
And to use it:
<Grid BackgroundColor="{x:Static local:Styles.BackgroundColor}"/>
Color can be set in String values instead of Hexa values.
Resources can be set in Window.Resources or Grid.Resources.
Resources in Window level
<Window.Resources>
<Color x:Key="TheBackgroundColor">#2196f3</Color>
</Window.Resources>
<Grid>
<Style TargetType="Grid">
<Style TargetType="Label">
<Setter Property="TextColor" Value="{StaticResource TheBackgroundColor}" />
</Style>
</Grid>
Resources in Grid level
<Grid>
<Grid.Resources>
<Color x:Key="TheBackgroundColor">#2196f3</Color>
</Grid.Resources>
<Style TargetType="Grid">
<Style TargetType="Label">
<Setter Property="TextColor" Value="{StaticResource TheBackgroundColor}" />
</Style>
</Grid>
what approach would you use if you have a mobile app build using xamarin and this app is like a vanilla app.
Each client should be able to apply their own style to labels etc.. theme
Basically all the views/pages are the same for all the clients but the style per page will change for each individual client.
If this wasn't a mobile app I would have plugins within a folder and depending on the client i would drop the relative plugin in the pluginFolder eg:pluginClientA etc.. and this would be discovered at runtime usin mef.
Any suggestions
Set you styles up as dynamicresources as seen here https://developer.xamarin.com/guides/xamarin-forms/user-interface/styles/dynamic/
I only have 2 themes so I have them specified in xaml
<ResourceDictionary>
<Color x:Key="Primary">#337ab7</Color>
<Color x:Key="Accent">#96d1ff</Color>
<Color x:Key="BackgroundColor">#C0C0C0</Color>
<Color x:Key="TextColor">White</Color>
<!-- Buttons -->
<Style x:Key="buttonStyle" TargetType="Button">
<Setter Property="BorderRadius" Value="4" />
<Setter Property="FontSize" Value="48" />
<Setter Property="FontAttributes" Value="Bold" ></Setter>
<Setter Property="HeightRequest">
<Setter.Value>
<OnPlatform x:TypeArguments="x:Double" iOS="80" Android="80" />
</Setter.Value>
</Setter>
<Setter Property="WidthRequest">
<Setter.Value>
<OnPlatform x:TypeArguments="x:Double" iOS="150" Android="150" />
</Setter.Value>
</Setter>
</Style>
<Style x:Key="btnPrimary" TargetType="Button" BasedOn="{StaticResource buttonStyle}">
<Setter Property="TextColor" Value="{DynamicResource TextColor}" />
<Setter Property="BackgroundColor" Value="{DynamicResource Primary}" />
</Style>
<!--Light Theme-->
<Color x:Key="Primary-Light">#2196F3</Color>
<Color x:Key="BackgroundColor-Light">#FAFAFA</Color>
<Color x:Key="TextColor-Light">White</Color>
<!--Dark Theme-->
<Color x:Key="Primary-Dark">#1976D2</Color>
<Color x:Key="BackgroundColor-Dark">#C0C0C0</Color>
<Color x:Key="TextColor-Dark">Black</Color>
</ResourceDictionary>
So in the theme change event you can change the assigned colours
Resources ["Primary"] = Resources ["Primary-Dark"];
I am creating a Windows Phone 8 app. In the app I have a ResourceDictionary defined in a XAML file as:
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="YellowGreen" />
<Setter Property="FontSize" Value="35" />
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
<Style TargetType="TextBox" x:Key="CommonSetters">
<Setter Property="Width" Value="450"/>
<Setter Property="FontSize" Value="35" />
<Setter Property="Foreground" Value="YellowGreen" />
<Setter Property="Height" Value="100"/>
<Setter Property="Background" Value="Red">
<!--<Setter.Value>
<ImageBrush ImageSource="logo.png" Opacity="0.1"/>
</Setter.Value>-->
</Setter>
</Style>
</ResourceDictionary>
This ResourceDictionary is referenced in App.xaml as:
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<!--Application Resources-->
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:Work_Force" x:Key="LocalizedStrings"/>
<ResourceDictionary x:Key="myDict">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<Style TargetType="TextBlock" x:Key="NN">
<Setter Property="Width" Value="450"/>
<Setter Property="FontSize" Value="35" />
<Setter Property="Foreground" Value="YellowGreen" />
<Setter Property="Height" Value="100"/>
</Style>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>
And then the easy part of doing:
Static Resources NN work fine but commonSetters is not working it declared in resourse.xaml.
The way you defined your resources in App.xaml, your 'myDict' resource dictionary is a nested dictionary within Application.Resources default dictionary. I don't know of any syntax how you can reference resources in nested dictionaries in XAML (as I didn't need this ever yet; I think FindResource(...) from code behind can accomplish this). A way to fix your problem is, that you modify the Application.Resources' default dictionary directly to merge with your 'Resources' dictionary. You can do so by modifying the Application.Resources section:
<Application.Resources>
<ResourceDictionary>
<local:LocalizedStrings xmlns:local="clr-namespace:PhoneApp1" x:Key="LocalizedStrings"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Note that now you don't have a ReosurceDictionary called 'myDict' anymore. Your 'Resources' dictionary will be directly merged into the default dictionary, that has no key. Then the resource will be resolved correctly.
finally i got the solution it similar like the aspx or html page, where we add the reference of the style in required page
in whereever you, it required to add the reference of the page in page where you have required
<phone:PhoneApplicationPage.Resources>
<ResourceDictionary Source="yourstylepage.xaml"/>
</phone:PhoneApplicationPage.Resources>
put the above code after <phone:PhoneApplicationPage>