使用字体时遇到麻烦。当我执行以下操作时,效果很好。
FontFamily="{StaticResource IconFonts}" Text=""
但是如果我尝试动态绑定text属性,它将不起作用。
FontFamily="{DynamicResource IconFonts}" Text="{Binding Comments}"
它显示出所有异常。看起来它尝试做某事,但是没有完全渲染。这是完整的代码,下面包含我的测试。预先感谢。
<FlexLayout x:Name="FlexFood"
Wrap="Wrap"
Direction="Row"
JustifyContent="Start"
Padding="0, 10"
BindableLayout.ItemsSource="{Binding FoodCollection}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Padding="5, 0">
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTypesSelected"></TapGestureRecognizer>
</Frame.GestureRecognizers>
<Frame.Content>
<StackLayout x:Name="LayoutFood">
<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<Label x:Name="LabelTitle" Padding="0, 10">
<Label.FormattedText>
<FormattedString>
<Span FontSize="Subtitle" Text="{Binding Title}" FontAttributes="Bold"></Span>
<Span Text=" "></Span>
<Span FontSize="Subtitle" Text="{Binding Version}" FontAttributes="Bold"></Span>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<StackLayout HorizontalOptions="StartAndExpand">
<Label Text="{Binding Description}" FontSize="Body"></Label>-->
<Label FontSize="Caption" TextColor="#LELELE">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding fDepartment}"></Span>
<Span Text="{Binding pType}"></Span>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
<StackLayout HorizontalOptions="End">
<Label Text="{Binding Comments}"></Label>
<Label FontFamily="{StaticResource IconFonts}" Text=""></Label>
<Label x:Name="LabelTypeTest" FontFamily="{StaticResource IconFonts}"
Text="{Binding Comments, StringFormat=''}"></Label>
<Label FontFamily="{DynamicResource IconFonts}" Text="{Binding Comments}"></Label>
<Image x:Name="ImageTypeTest">
<Image.Source>
<FontImageSource Size="48" FontFamily="{StaticResource IconFonts}" Color="#000000"></FontImageSource>
</Image.Source>
</Image>
</StackLayout>
</StackLayout>
</StackLayout>
</Frame.Content>
</helper:LayoutGradient>
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>
您如何设置动态资源?我相信它必须用代码完成,而不是XAML。请参阅以下文档:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/dynamic
例如(以防关联文档消失):
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.DynamicStylesPage" Title="Dynamic" IconImageSource="xaml.png">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="baseStyle" TargetType="View">
...
</Style>
<Style x:Key="blueSearchBarStyle"
TargetType="SearchBar"
BasedOn="{StaticResource baseStyle}">
...
</Style>
<Style x:Key="greenSearchBarStyle"
TargetType="SearchBar">
...
</Style>
...
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout Padding="0,20,0,0">
<SearchBar Placeholder="These SearchBar controls"
Style="{DynamicResource searchBarStyle}" />
...
</StackLayout>
</ContentPage.Content>
</ContentPage>
后面的代码:
public partial class DynamicStylesPage : ContentPage
{
bool originalStyle = true;
public DynamicStylesPage ()
{
InitializeComponent ();
Resources ["searchBarStyle"] = Resources ["blueSearchBarStyle"];
}
void OnButtonClicked (object sender, EventArgs e)
{
if (originalStyle) {
Resources ["searchBarStyle"] = Resources ["greenSearchBarStyle"];
originalStyle = false;
} else {
Resources ["searchBarStyle"] = Resources ["blueSearchBarStyle"];
originalStyle = true;
}
}
}
您可以在上面看到searchBarStyle
是如何创建和用作DynamicResource的,以及如何更改动态资源正在使用的基础资源。
但是,在没有看到更多代码的情况下,我不知道您是否已经进行了上述操作。