Search code examples
xamlxamarinxamarin.formsstaticresourceapp.xaml

How to use a nested string in App.xaml static resources...?


In my App.xaml file, I have the following static resources defined...

<x:String x:Key="StaticString1">static string 1</x:String>
<x:String x:Key="StaticString2">static string 2 using {StaticResource StaticString1}</x:String>

In the content view (on another page), I want to display StaticString2 and have it automatically pull in StaticString1 but it isn't working. I want it to say "static string 2 using static string 1" but instead it just shows a literal with the curly braces ("static string 2 using {StaticResource StaticString1}").

Is it possible to do this in static resources or do I need to use a <Label.FormattedText> with <Span>s ?


Solution

  • No, I don't think you can combine two strings in the xaml.

    You can use <Label.FormattedText> with <Span> as you mentiond to achieve this:

        <ContentPage.Content>
    
            <StackLayout>
                <Label >
                    <Label.FormattedText>
                        <FormattedString>
                            <Span TextColor="Black" FontSize="18" Text="{StaticResource StaticString2}"/>
                            <Span TextColor="Black" FontSize="18" Text=" "/>
                            <Span TextColor="Black" FontSize="18" Text="{StaticResource StaticString1}"/>
                        </FormattedString>
                    </Label.FormattedText>
                </Label>
            </StackLayout>
            
        </ContentPage.Content>
    

    And in App.xaml:

        <x:String x:Key="StaticString1">static string 1</x:String>
        <x:String x:Key="StaticString2">static string 2 using </x:String>