Assigning a Style in custom WP renderers

STTDevelopersFrontEndSTTDevelopersFrontEnd ZAMember ✭✭
edited August 2015 in Xamarin.Forms

I've created a custom render for my switches in WP, in this render I assign a style I've defined in the App.xaml

<Style x:Key="ToggleSwitchWithMargin" TargetType="controls:ToggleSwitch"> <Setter Property="Background" Value="{StaticResource PhoneBackgroundBrush}"/> <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyLight}"/> <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeLarge}"/> <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> <Setter Property="IsTabStop" Value="False"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="SwitchForeground" Value="{StaticResource PhoneAccentBrush}"/> <Setter Property="VerticalContentAlignment" Value="Top"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="controls:ToggleSwitch"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CacheMode="BitmapCache" Padding="{TemplateBinding Padding}"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="Disabled"> <Storyboard> <DoubleAnimation Duration="0" To="0.3" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Header"/> <DoubleAnimation Duration="0" To="0.3" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Content"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid Margin="12,5,12,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ContentControl x:Name="Header" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Foreground="{StaticResource PhoneSubtleBrush}" FontSize="{StaticResource PhoneFontSizeNormal}" FontFamily="{StaticResource PhoneFontFamilyNormal}" HorizontalAlignment="Left" IsTabStop="False" Margin="-1,0,0,0" Opacity="{TemplateBinding Opacity}" VerticalAlignment="Bottom"/> <ContentControl x:Name="Content" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsTabStop="False" Margin="-1,1,0,-7" Opacity="{TemplateBinding Opacity}" Grid.Row="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> <toolkitPrimitives:ToggleSwitchButton x:Name="Switch" Background="{TemplateBinding Background}" Grid.Column="1" Margin="-22,-29,-24,-28" Opacity="{TemplateBinding Opacity}" Grid.RowSpan="2" SwitchForeground="{TemplateBinding SwitchForeground}" VerticalAlignment="Bottom"/> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>

To assign this style i use the following code:
var style = App.Current.Resources["ToggleSwitchWithMargin"] as System.Windows.Style; Control.Style = style;

But once the render runs i get the following exception______
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

Is this the work way to assign styles?

Best Answer

Answers

  • PaulDistonPaulDiston USUniversity ✭✭✭✭

    Hi,

    Where are you calling the following code in your renderer :-

    var style = App.Current.Resources["ToggleSwitchWithMargin"] as System.Windows.Style; Control.Style = style;

    Thanks

    Paul Diston

  • @PaulDiston
    Im calling this in the OnElementChanged, that I had overridden from the Xamarin SwitchRenderer

  • @jimmgarr Thanks that helped a lot.
    Fixed the issue perfectly after I edited my style for a border

Sign In or Register to comment.