应用资源无法使用静态资源/静态扩展



美好的一天!我正在使用Prism 7.2和Xamarin.Forms 4.2,并且我正在尝试使用字体的外部资产。我已经在我的应用资源上定义了它们,并在我的 xaml 代码上使用它,但是,当我尝试将静态扩展{x:Static}用于Text的值与Style结合使用时,{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>

并在xaml.cs上调用 testLabel,使其具有将起作用的值。

知道我做错了什么吗?编译时找不到任何错误。

好吧,这是我的错。

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构造函数上,并且资源按预期工作。

最新更新