How to set FontAttributes within XAML to both Bold and Italic? - xaml

In Xamarin.Forms, how do I set FontAttributes within XAML to both Bold and Italic?
Example:
<Style TargetType="Label">
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="FontAttributes" Value="Italic" />
</Style>

<Style TargetType="Label">
<Setter Property="FontAttributes" Value="Bold, Italic" />
</Style>
FontAttributes is a Flag, so you can pass multiple values.

If anyone is looking for the code-behind solution:
element.FontAttributes = FontAttributes.Bold | FontAttributes.Italic;

Just keep use commas to separate them in the value field.
<Style TargetType="Label">
<Setter Property="FontAttributes" Value="Bold, Italic"/>
</Style>
Make sure to review http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/fonts/

Related

change barcolor Flyout xamarin xaml

somebody knows how to change the bar color, that I point out in the image
bar flyout
If you want to change the background color of Navigation Bar in shell , we could set the style of Shell navigation bar in Resource Dictionary .
<Shell.Resources>
<ResourceDictionary>
<Color x:Key="NavigationPrimary">#2196F3</Color>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="Red" /> // set navigation bar color here
<Setter Property="Shell.ForegroundColor" Value="Blue" />
<Setter Property="Shell.TitleColor" Value="Blue" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource NavigationPrimary}" />
<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}" />
</ResourceDictionary>
</Shell.Resources>
For NavigationPage (I assume your detail page is composed of a NavigationPage and inside your HomePage or CurrentNavigatedPageFromMenu, here is a simple style :
<Style x:Key="NavigationPageStyle" TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="Red" />
<Setter Property="BarTextColor" Value="White" />
</Style>
<Style BasedOn="{StaticResource NavigationPageStyle}" TargetType="NavigationPage" />
I separate them to allow to reuse the style on other Navigation page control I've extended.
In code behind, in the constructor of your page (can be usefull to provide a different color depend on the page you are, exemple : red for error page, green for parameter, blue for others).
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Red;
((NavigationPage)Application.Current.MainPage).BarTextColor = Color.White;
You can find some usefull tips [Xamarin Forum too][1].

Xamarin Forms Styling using StyleClass vs Style attribute

We're building a Xamarin Forms app we've noticed we could style an element in 2 ways by creating styles in the App.xaml ResourceDictionary
Class and StyleClass option
In App.xaml we'll write
<Style Class="EntryStandard" TargetType="Entry">
<Setter Property="TextColor" Value="#575e62" />
<Setter Property="BackgroundColor" Value="#9facb3" />
<Setter Property="FontSize" Value="14" />
</Style>
Then this gets used in one of the contentpages like this
<Entry StyleClass="EntryStandard" Placeholder="Login Name" Text="{Binding EntryEmailAddress}" />
Key and Style option
This is what we write under App.xaml
<Style x:Key="ButtonMainMenu_Purple" TargetType="Button">
<Setter Property="BackgroundColor" Value="#5d4785" />
<Setter Property="FontSize" Value="14" />
<Setter Property="TextColor" Value="#FFFFFF" />
</Style>
And then we use the following in our contentpages
<Button Style="{StaticResource ButtonMainMenu_Purple}" Text="Friends" Command="{Binding OnFriendsButtonCommand}" />
Both work fine, I just wanted to know which one is better than the other and why?
Regular styles follow the standard, relatively inflexible WPF model. Style classes includes cascade semantics and are part of the new theme support. They are poorly documented, however, and still in beta.

how to use Multiple attribute in StaticResource(uwp)?

How to use multiple key(used in ResourceDictionary) in Style="{StaticResource
TopHeader }"
You can combile your multiple styles inside new created style.
For example:
<Style x:Key="Style1" TargetType="Button">
<Setter Property="Background" Value="Yellow" />
</Style>
<Style x:Key="Style2" TargetType="Button">
<Setter Property="Foreground" Value="Red" />
</Style>
<Style x:Key="Style1Style2" TargetType="Button">
<Setter Property="Background" Value="Yellow" />
<Setter Property="Foreground" Value="Red" />
</Style>
or you can create BasedOn style:
<Style x:Key="Style3" TargetType="Button" BasedOn="{StaticResource Style2}">
<Setter Property="Background" Value="Yellow" />
</Style>

Windows Store app failed to create style BasedOn shared style

I have a windows 8.1 app.
In Project.Shared there is ResourceDictionary SharedResources.xaml with base style,
<Style x:Key="CommonLayerListItemStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe WP" />
<Setter Property="Foreground" Value="{StaticResource UnselectBrush}" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
in Windows Phone app is StyleResources.xaml wiht style based on this
<Style x:Key="LayerListItemStyle"
BasedOn="{StaticResource CommonLayerListItemStyle}"
TargetType="TextBlock">
<Setter Property="FontSize" Value="20" />
</Style>
same in Windows app StyleResources.xaml:
<Style x:Key="LayerListItemStyle"
BasedOn="{StaticResource CommonLayerListItemStyle}"
TargetType="TextBlock">
<Setter Property="FontSize" Value="28" />
<Setter Property="Margin" Value="42,0,0,0" />
</Style>
All styles are used in UserControl created in Shared project.
I do this to override FontSize on diffrent platforms.
I merged all dictionaries in App.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/SharedResources.xaml" />
<ResourceDictionary Source="Styles/StyleResources.xaml" />
</ResourceDictionary.MergedDictionaries>
But my app does not start with Unhandled exception
Cannot find a Resource with the Name/Key CommonLayerListItemStyle [Line: 10 Position: 37]
Why does this happens?
I had the same issue. You need to merge the SharedResources.xaml directly into StyleResources.xaml, and only reference StyleResources.xaml from App.xaml.
Here is a complete example:
http://blog.craftingbytes.com/2015/05/resource-sharing-in-windows-universal.html

MergedDictionaries can not be referenced from App.xaml

My styles are separated in separate files for better organization and readability. All files are referenced in my App.xaml. Some styles are BasedOn other styles, so I have placed them in the proper order so that the inherited styles are loaded before the dependent ones. However, I am still getting the following error on my XAML views that reference a font style, for example...Cannot find a Resource with the Name/Key SourceSansPro-Light.
Can someone enlighten me on why this Name/Key cannot be found? Here's the relevant code.
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Common/Styles/StyleResources.xaml" />
<ResourceDictionary Source="Common/Styles/InputStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
StyleResources.xaml
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="TextBoxBackgroundBrush" Color="White"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
<FontFamily x:Key="SourceSansPro-Light">../../Assets/Fonts/SourceSansPro-Light.otf#Source Sans Pro</FontFamily>
<Style x:Key="TextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource BaseTextBlockStyle}">
<Setter Property="FontSize" Value="18"/>
<Setter Property="FontFamily" Value="{StaticResource SourceSansPro-Semibold}" />
</Style>
InputStyles.xaml
<Style x:Key="SecondaryTextStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockStyle}">
<Setter Property="FontFamily" Value="{StaticResource SourceSansPro-Light}"/>
</Style>
HomePage.xaml (where style, SourceSansPro-Light, is being called in this view)
<Page.Resources>
<Style x:Name="InfoBodyTextStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="{StaticResource SourceSansPro-Light}"/>
</Style>
Please try this code.
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="#FF64CCEF"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="FontFamily" Value="{DynamicResource FontFamilyResource}"/>
</Style>
<FontFamily x:Key="FontFamilyResource">/ApplicationName(SolutionName);component/Resources/Fonts/#Vladimir Script</FontFamily>