How is it proposed to inherit from implicit style in Xamarin.Forms XAML?

I am having some problems inheriting from the implicit style defined in Application ResourceDictionary. I tried several different approaches, but each of them was unsuccessful in one form or another.

Attempt 1: Inherit from Implicit Style

Application ResourceDictionary

<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>

ResourceDictionary Page

<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>

Page Control

<Button Style="{StaticResource LoginButtonStyle}" />

Result

Throws an exception "StaticResource not found for key {x: Button Type}". This is how I remember how it was done in WPF, but I assume that it is not supported by Xamarin XAML.

Attempt 2: Inherit from a common explicit style

Application ResourceDictionary

<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 commented out, everything works fine. -->
<Style TargetType="Button" BasedOn="{StaticResource DefaultButtonStyle}" />

ResourceDictionary Page

<Style x:Key="LoginButtonStyle" TargetType="Button" BasedOn="{StaticResource DefaultButtonStyle}">
    <!-- Same as Attempt 1 -->
</Style>

Page Control

<!-- Same as Attempt 1 -->

Result

, . , , Trigger . . , , , .

?

Xamarin

+4
1

, !

-, , :

<!-- 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>

, , , , . BaseButtonStyle, .

: .

. -, , . , , .

+2

Source: https://habr.com/ru/post/1653038/


All Articles