Create a style BasedOn default style defined in app's global resource dictionary

nerowareneroware USMember ✭✭

Hi there. I have the following style defined in my app's global resource dictionary:

<Style TargetType="Label">
    <Setter Property="FontFamily" Value="Open Sans" />
</Style>

And this style defined in a page:

<Style x:Key="ItemBaseStyle" TargetType="Label">
    <Setter Property="HorizontalOptions" Value="Start" />
    <Setter Property="VerticalOptions" Value="Center" />
    <Setter Property="FontSize" Value="14" />
</Style>

I would expect that the default style's FontFamily value would apply to ItemBaseStyle, but it doesn't. I also tried the following, but it threw an exception:

<Style x:Key="ItemBaseStyle" TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
    ...
</Style>

So, do I have to redefine the FontFamily value on the style in my page? Or how do default styles apply to styles elsewhere?

Best Answer

Answers

  • midixmidix LVMember ✭✭

    Unfortunately this seems to be not working for custom controls. Simplified example:

        <Application
            xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:cntrls="clr-namespace:MyAssembly.UI.Controls;assembly=MyAssembly"
            x:Class="My.App">
    
          <Application.Resources>
                <ResourceDictionary>
    
                    <Style TargetType="cntrls:MyButton">
                        <Setter Property="TextColor" Value="Red" />
                    </Style>
    
                    <Style x:Key="MyStyle" TargetType="cntrls:MyButton" BasedOn="{StaticResource MyAssembly.UI.Controls.MyButton}">
                        <Setter Property="FontSize" Value="14" />
                    </Style> 
    
                </ResourceDictionary>
           </Application.Resources>
        </Application>
    

    I tried also
    BasedOn="{StaticResource MyButton}" and
    BasedOn="{StaticResource cntrls:MyButton}" and
    BasedOn="{StaticResource {x:Type MyButton}}" and
    BasedOn="{StaticResource {x:Type cntrls:MyButton}}" and
    BasedOn="{StaticResource {x:Type MyAssembly.UI.Controls.MyButton}}"

    but all of them threw
    Xamarin.Forms.Xaml.XamlParseException: Position 23:76. StaticResource not found for key ...

  • ShaneNeuvilleShaneNeuville USUniversity ✭✭
    edited October 2016

    @midix Does this work?

    `

      <Application.Resources>
            <ResourceDictionary>
    
                <Style TargetType="cntrls:MyButton" x:Key="MyButtonBaseStyleKey">
                    <Setter Property="TextColor" Value="Red" />
                </Style>
    
                <Style x:Key="MyStyle" TargetType="cntrls:MyButton" BasedOn="{StaticResource MyButtonBaseStyleKey}">
                    <Setter Property="FontSize" Value="14" />
                </Style> 
    
            </ResourceDictionary>
       </Application.Resources>
    </Application>
    

    `

  • rio_riyario_riya INMember ✭✭

    have anyone found solution to this?

  • sgousgou DEMember ✭✭

    This one should work!

      <Application.Resources>
            <ResourceDictionary>
    
                <Style TargetType="cntrls:MyButton">
                    <Setter Property="TextColor" Value="Red" />
                </Style>
    
                <Style x:Key="MyStyle" TargetType="{x:Type cntrls:MyButton}" BasedOn="{StaticResource MyAssembly.UI.Controls.MyButton}">
                    <Setter Property="FontSize" Value="14" />
                </Style> 
    
            </ResourceDictionary>
       </Application.Resources>
    </Application>
    
Sign In or Register to comment.