Xamarin.Forms - 共享库中的StaticResource

时间:2018-04-05 09:37:37

标签: c# xamarin xamarin.forms resourcedictionary

我创建了一个共享库项目,它定义了一些常用的视图。例如:

<Label x:Name="lblIcon"
    FontSize="30" 
    Margin="20,0,5,0"
    WidthRequest="30"
    VerticalOptions="Center" />

现在重用那个样式,我在App.Xaml中定义了这个样式(仍在共享项目中),并想通过StaticResource引用它:

<Label x:Name="lblIcon"
    Style="{StaticResource TheLabelsStyle}"
    VerticalOptions="Center" />

但是这会引发UnhandledException而没有进一步的信息。

希望有人看到我的错误,可以帮助我。提前谢谢!

EDIT1

这是风格:

<Style x:Key="TheLabelsStyle" TargetType="Label">
    <Setter Property="TextColor" Value="White"/>
    <Setter Property="FontSize" Value="30" />
    <Setter Property="FontAttributes" Value="Bold" />
</Style>

编辑2

多次清理和重建解决方案后,我发现问题来自合并的词典,尤其是FontFamilies。

<!-- App.xaml inside the Shared Project -->
<Application.Resources>

    <!-- Application resource dictionary -->
    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <styles:FontFamilies />
            <styles:LabelStyles />
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

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>

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>

但如果我做的话......

<Label x:Name="lblIcon"
    FontSize="30" 
    FontFamily="{DynamicResource FontAwesome}"
    Margin="20,0,5,0"
    WidthRequest="30"
    VerticalOptions="Center" />

......这是有效的

0 个答案:

没有答案