How to specify the path to an inner property of a resource in xaml?

GaetanFGaetanF USMember ✭✭✭

I have a composite object stored as a global resource and I would like to access to one of its property in xaml.
I tried to do that:

    <ResourceDictionary>
        <models:ColorPair x:Key="myColorPair" x:FactoryMethod="FromHex">
            <x:Arguments>
                <x:String>#fea70a</x:String>
                <x:String>#fea70a</x:String>
            </x:Arguments>
        </models:ColorPair>

        ...

        <Style x:Key="ButtonStyle" TargetType="Button">
            <Setter Property="BackgroundColor" Value="{StaticResource myColorPair.BackgroundColor}" />
            <Setter Property="TextColor" Value="{StaticResource myColorPair.ForegroundColor}" />
        </Style>
        <!-- Application resource dictionary -->
    </ResourceDictionary>

But I got an exception: Exception thrown: 'Xamarin.Forms.Xaml.XamlParseException', Message: Position ##:##. StaticResource not found for key myColorPair.BackgroundColor

Is that possible?

Answers

  • MulflarMulflar ESUniversity ✭✭
    edited December 2017

    Isn't easy to do it like this?

    <ResourceDictionary>
                <Color x:Key="backColor">#fea70a</Color>
                <Color x:Key="foreColor">#fea70a</Color>
    
        <Style x:Key="ButtonStyle" TargetType="Button">
                  <Setter Property="BackgroundColor" Value="{StaticResource backColor}" />
                 <Setter Property="TextColor" Value="{StaticResource foreColor}" />
             </Style>
    <!-- Application resource dictionary -->
        </ResourceDictionary>
    
  • GaetanFGaetanF USMember ✭✭✭

    Because I have a bunch of them, I would prefer not, because both colours are paired and does not really mean anything if separated. Maybe I could specify the relation in the names instead of having a class handling it but that's quite verbose. Anyway, I guess it is not possible to specify a property in the static resource binding?

Sign In or Register to comment.