Search code examples
xamarin.formssyncfusion

Xamarin.Forms - Using StaticResource inside ResourceDictionary


I have a little app in which i have a ContentPage with Syncfusion's SfChat, im trying to customize it a bit, so im using a ResourceDictionary like this:

<ContentPage.Resources>
        <syncTheme:SyncfusionThemeDictionary>
            <syncTheme:SyncfusionThemeDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <x:String x:Key="SfChatTheme">CustomTheme</x:String>
                    <x:String x:Key="SfChatIncomingMessageAuthorFontFamily">MontserratRegular</x:String>
                </ResourceDictionary>
            </syncTheme:SyncfusionThemeDictionary.MergedDictionaries>
        </syncTheme:SyncfusionThemeDictionary>
    </ContentPage.Resources>

My problem is that the "SfChatIncomingMessageAuthorFontFamily" property needs a font from my app's resources as a static resource, so how do i use {StaticResource MontserratRegular} instead of just passing the font's name as an x:string ?


Solution

  • For exampl,use it on a label:

    <ResourceDictionary>
        <OnPlatform
          x:Key="MediumFontFamily"
          x:TypeArguments="x:String"
          Android="sans-serif-medium"
          iOS="HelveticaNeue-Medium" />
    
    </ResourceDictionary>
    

    create styles :

    <Style x:Key="MyLabel" TargetType="Label">
       <Setter Property="FontFamily"
            Value="{StaticResource MediumFontFamily}" />
    </Style>
    

    then use in label:

    <Label Style="{StaticResource MyLabel}" Text="Hello World" />