Set StaticResource style of a control in code behind
It has been more than 4 years now since this question was asked, but I want to post an answer just to share my findings.
For example if there is a Style
BlueButton
described in Application resource in App.xaml
(Xamarin Cross-Platform App development), it can be used as follows
<?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="SharedUi.App">
<Application.Resources>
<ResourceDictionary>
<Style x:Key="BlueButton" TargetType="Button">
<Setter Property="TextColor" Value="White" />
<Setter Property="FontSize" Value="20" />
<Setter Property="BackgroundColor" Value="Blue"/>
<Setter Property="HeightRequest" Value="70"/>
<Setter Property="FontAttributes" Value="Bold"/>
</Style>
</ResourceDictionary>
</Application.Resources></Application>
Then in the code behind
Button newButton1 = new Button
{
Text = "Hello",
WidthRequest = (double)15.0,
Style = (Style)Application.Current.Resources["BlueButton"]
};
You can set, Something like this,
TextBlock myTextBlock= new TextBlock ()
{
FontFamily = new FontFamily("Segoe UI Light");
Style = Resources["TextBlockStyle"] as Style,
};