首页 > 解决方案 > Xamarin.Forms - 在 ResourceDictionary 中使用 StaticResource

问题描述

我有一个小应用程序,其中有一个带有 Syncfusion 的 SfChat 的 ContentPage,我试图对其进行一些自定义,所以我使用这样的 ResourceDictionary:

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

我的问题是“SfChatIncomingMessageAuthorFontFamily”属性需要我的应用程序资源中的字体作为静态资源,那么我如何使用 {StaticResource MontserratRegular} 而不仅仅是将字体名称作为 x:string 传递?

标签: xamarin.formssyncfusion

解决方案


例如,在标签上使用:</p>

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

</ResourceDictionary>

创建样式:

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

然后在标签中使用:

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

推荐阅读