I am having some issues inheriting from an implicit style defined in an Application ResourceDictionary. I have tried a couple of different approaches, but each has been unsuccessful in one form or another.
<Style TargetType="Button">
<Setter Property="BackgroundColor" Value="#1386F1" />
<Setter Property="TextColor" Value="White" />
<Setter Property="FontAttributes" Value="Bold" />
<Setter Property="HeightRequest">
<OnPlatform x:TypeArguments="x:Double" iOS="30" Android="50" />
</Setter>
<Style.Triggers>
<Trigger TargetType="Button" Property="IsEnabled" Value="False">
<Setter Property="BackgroundColor">
<OnPlatform x:TypeArguments="Color" iOS="#E8E8E8" />
</Setter>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="LoginButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="BackgroundColor" Value="#024886" />
<Setter Property="Margin">
<OnPlatform x:TypeArguments="Thickness" iOS="0,5" />
</Setter>
</Style>
<Button Style="{StaticResource LoginButtonStyle}" />
Throws exception "StaticResource not found for key {x:Type Button}". This is how I remember doing it in WPF, but I'm assuming this isn't supported in Xamarin XAML.
<Style x:Key="DefaultButtonStyle" TargetType="Button">
<!-- Same as Attempt 1 -->
</Style>
<!-- This implicit style causes issues with the inheritence of the Trigger in the above explicit style. When it's commented out, everything works fine. -->
<Style TargetType="Button" BasedOn="{StaticResource DefaultButtonStyle}" />
<Style x:Key="LoginButtonStyle" TargetType="Button" BasedOn="{StaticResource DefaultButtonStyle}">
<!-- Same as Attempt 1 -->
</Style>
<!-- Same as Attempt 1 -->
Everything works except for the Trigger. However, when I remove the implicit style the Trigger magically starts working again. I'd rather not have to specify the explicit style on every Button, though. I believe this is just a bug, but I wanted to make sure before I take the time to prepare a bug report.
Any ideas?
Question in Xamarin Forums
First of all, thanks for the nice summary!
In my opinion this is a bug, which can be prevented with a little trick:
<!-- Base Style -->
<Style x:Key="BaseButtonStyle" TargetType="Button">
<!-- Define base button style... -->
</Style>
<!-- Implicit Style -->
<Style TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"></Style>
<!-- Inherited Style -->
<Style x:Key="InheritedButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}">
<!-- Extend base button style... -->
</Style>
Basically, the idea is to create a common explicit base style, which gets inherited by an implicit style afterwards. Therefore, all buttons are automatically applying the BaseButtonStyle, which is additionally extensible.
Attention: The implicit style has to be created after defining the base style.
EDIT: Apparently this is the same solution as #2 already provided before. So, the Trigger issue is not fixed, which seems to be a bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With