首页 > 解决方案 > 应用程序资源无法使用静态资源/静态扩展

问题描述

再会!我正在使用 Prism 7.2 和 Xamarin.Forms 4.2,并且我正在尝试将外部资产用于字体。我已经在我的应用程序资源上定义了它们并在我的 xaml 代码上使用了它,但是,当我尝试使用静态扩展名作为结合{x:Static} 的值时,它也有一个静态资源扩展名,文本似乎不起作用。下面是我正在使用的代码。TextStyle{StaticResource}

 <prism:PrismApplication.Resources>
    <ResourceDictionary>
        <OnPlatform x:Key="LineAwesomeFontFamily" x:TypeArguments="x:String">
            <On Platform="Android" Value="line-awesome.ttf#LineAwesome"/>
            <On Platform="iOS" Value="lineawesome"/>
        </OnPlatform>
        <Style x:Key="LineAwesomeFonts"
               TargetType="Label">
            <Setter Property="FontFamily"
                Value="{StaticResource LineAwesomeFontFamily }" />
        </Style>

        <Style TargetType="Button">
            <Setter Property="BackgroundColor" Value="Green"/>
        </Style>
    </ResourceDictionary>

</prism:PrismApplication.Resources>

在视图中调用它

<Label Text="{x:Static fonts:LineAwesomeFonts.Adjust}"
       Style="{StaticResource LineAwesomeFonts}"
       TextColor="Black">
</Label>

如果我尝试不同的方式

 <Label x:Name="testLabel"
        FontSize="28" TextColor="Black">
        <Label.FontFamily>
            <OnPlatform x:TypeArguments="x:String">
                <On Platform="Android" Value="line-awesome.ttf#LineAwesome"/>
            </OnPlatform>
        </Label.FontFamily>
  </Label>

并调用 testLabelxaml.cs以使其具有可以工作的值。

知道我在做什么错吗?编译时没有发现任何错误。

标签: xamarin.formsprism

解决方案


好吧,这是我的错。

 public App(IPlatformInitializer platformInitializer) : base(platformInitializer)
 {}


 protected override void OnInitialized()
 {
     InitializeComponent(); //moved this away from App constructor and put it here
     NavigationService.NavigateAsync("Test");
 }

虽然这很奇怪,因为我InitializeComponent();在另一个项目上是在App构造函数上,并且资源按预期工作。


推荐阅读