xamarin - 我可以在资源而不是渲染器中定义不同的文本样式吗?
问题描述
目前我正在使用这个渲染器来设置文本大小的样式:
<local:StyledLabel YAlign="Center" Text="Buttons" HorizontalOptions="StartAndExpand" />
public class StyledLabel : Label
{
public StyledLabel()
{
if (Device.RuntimePlatform == Device.Android)
{
Style = Device.Styles.CaptionStyle;
}
else if (Device.RuntimePlatform == Device.iOS)
{
Style = Device.Styles.ListItemTextStyle;
}
}
}
有没有办法可以在资源 XAML 中执行类似的操作,以便 iOS 或 Android 的大小可能不同?
解决方案
在 XAML 中,您可以指定不同的值,如下所示:
<local:StyledLabel YAlign="Center" Text="Buttons" HorizontalOptions="StartAndExpand" />
<local:StyledLabel.Style>
<OnPlatform x:TypeArguments="Style">
<OnPlatform.Platforms>
<On Platform="iOS" Value="{StaticResource CaptionStyle}" />
<On Platform="Android" Value="{StaticResource ListItemTextStyle}" />
</OnPlatform.Platforms>
</OnPlatform>
</local:StyledLabel.Style>
...
</local:StyledLabel>
此处的更多信息:https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/essential-xaml-syntax
顺便说一句,YAlign
已弃用,您应该VerticalTextAlignment
.
推荐阅读
- ios - 离子 4 - livereload 不起作用
- ethereum - Solidity / Oraclize API out of gas 异常
- javascript - html和javascript中的多字段类型提前搜索框
- io - 通过 Power BI 发布报表直接输入数据
- jira - Jira - 在“完成”列中移动 5 天后披露票证的 JQL
- oauth-2.0 - OAuth 2:authorization_code Grant - 是否需要 client_secret 参数?
- android-fragments - 直接从按钮 Xamarin.Android 通过片段更改片段活动
- python - 使用多维 np.array 进行迭代
- r - 更改 ggplot2 中的刻度线标签
- ios - Swift - 三元条件运算符的误解