问题
I've made a Shared Library Project which defines some often used Views. For example:
<Label x:Name="lblIcon"
FontSize="30"
Margin="20,0,5,0"
WidthRequest="30"
VerticalOptions="Center" />
Now to reuse that style, I defined that style inside App.Xaml (still inside the shared Project) and want to reference it via StaticResource:
<Label x:Name="lblIcon"
Style="{StaticResource TheLabelsStyle}"
VerticalOptions="Center" />
But this throws an UnhandledException with no further information.
Hopefully someone sees my mistakes and could help me. Thanks in advance!
Edit1
Here is the Style:
<Style x:Key="TheLabelsStyle" TargetType="Label">
<Setter Property="TextColor" Value="White"/>
<Setter Property="FontSize" Value="30" />
<Setter Property="FontAttributes" Value="Bold" />
</Style>
Edit 2
After cleaning and rebuilding the solution multiple times, I figured out the issue comes from the merged Dictionary, especially the FontFamilies.
<!-- App.xaml inside the Shared Project -->
<Application.Resources>
<!-- Application resource dictionary -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<styles:FontFamilies />
<styles:LabelStyles />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The LabelStyles:
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:styles="clr-namespace:SharpLibrary.Forms.Source.Styles;assembly=SharpLibrary.Forms"
x:Class="SharpLibrary.Forms.Source.Styles.LabelStyles">
<Style x:Key="HeaderLabel" TargetType="Label">
<Setter Property="TextColor" Value="White"/>
<Setter Property="FontSize" Value="30" />
<Setter Property="FontAttributes" Value="Bold" />
</Style>
<Style x:Key="IconedRoundedEntryIconStyle" TargetType="Label">
<Setter Property="FontFamily" Value="{DynamicResource FontAwesome}" />
</Style>
</ResourceDictionary>
The FontFamilies:
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SharpLibrary.Forms.Source.Styles.FontFamilies">
<OnPlatform x:Key="FontAwesome" x:TypeArguments="x:String" >
<OnPlatform.Platforms>
<On Platform="iOS" Value="FontAwesome"/>
<On Platform="UWP" Value="ms-appx:///SharpLibrary.Forms/Assets/Fonts/fontawesome-webfont.ttf#FontAwesome"/>
</OnPlatform.Platforms>
</OnPlatform>
</ResourceDictionary>
But if I do something like...
<Label x:Name="lblIcon"
FontSize="30"
FontFamily="{DynamicResource FontAwesome}"
Margin="20,0,5,0"
WidthRequest="30"
VerticalOptions="Center" />
... this works
来源:https://stackoverflow.com/questions/49669020/xamarin-forms-staticresource-in-shared-library