问题描述
我正在尝试修改默认的 Xamarin Forms (Flyout) 应用模板,以将 Material Design 图标用于 FlyoutItem
图标,而不是提供的 .png
文件。我曾尝试关注 this blog post by James Montemagno,并尝试使用他的 Hanselman.Forms 项目作为参考...但我遗漏了一些东西。
注意:此时,我无法使用 iPhone 或 Mac,所以我只专注于 Android 项目。
我已经完成了以下步骤:
- 将
materialdesignicons-webfont.ttf
文件导入 Assets 文件夹并仔细检查其Build Action
是否设置为AndroidAsset
。 - 在
App.xaml
文件中添加了以下内容:
<OnPlatform x:Key="MaterialFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="materialdesignicons-webfont.ttf#Material Design Icons" />
</OnPlatform>
<x:String x:Key="IconAbout"></x:String>
- 修改了 AppShell.xaml 以更改图标:
<FlyoutItem Title="About">
<FlyoutItem.Icon>
<FontimageSource Glyph="{StaticResource IconAbout}"
FontFamily="{StaticResource MaterialFontFamily}"
Color="Black"/>
</FlyoutItem.Icon>
<ShellContent Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
</FlyoutItem>
这是从 Hanselman.Forms
项目的 TabItem
直接复制的 - 我认为 FontFamily 必须是 DynamicResource
,但显然不是,因为它按原样工作 在该项目中,但在我的任何一种方式中都不起作用 - 图标始终为空白(实际上,如果我将 Glyph
更改为字母 A
,我会得到一个“A”图标,但这并不是很有帮助)。
我遗漏了一些我看不到的愚蠢的小细节。
解决方法
我已经开始工作了。
首先,转到这个 GitHub 存储库并将此材料字体系列下载为 .tff 文件。单击存储库中的“字体”文件并下载 MaterialIcons-Regular.ttf
。
链接:https://github.com/google/material-design-icons
现在像这样引用 assemblyinfo.cs 文件中的字体:
[assembly: ExportFont("MaterialIcons-Regular.ttf",Alias = "Material")]
在 .tff 文件中将构建操作设置为 'embedded resource'。不要忘记这一步!
转到此页面 https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.codepoints - 查看所有材料代码点。
在这种情况下,我将使用“alarm-add”图标作为示例(因为为什么不呢)。如果您想使用“alarm-add”图标 - 找到“alarm-add”代码点。在这种情况下,“alarm-add”图标的代码点的代码是e856
。
将以下代码粘贴到您的 shell 中:
<ShellContent Title="About"
ContentTemplate="{DataTemplate local:AboutPage}"
>
<ShellContent.Icon>
<FontImageSource FontFamily="Material"
Color="White"
Glyph="">
</FontImageSource>
</ShellContent.Icon>
</ShellContent>
如果您按照所有步骤进行操作 - 结果应该如下所示:
如果 - 在任何情况下 - 您想在外壳之外使用材质图标,您可以创建一个标签,其字体系列设置为 Material
和 {{1}属性您可以设置适当的代码点。请记住在代码点之前包含 Text
- 并始终以分号结束。
您可以根据自己的喜好自定义颜色和图标 - 只需在代码点文档中搜索您想要的图标,然后粘贴标识该特定图标的代码点即可。
,错误的根源在这里:
<x:String x:Key="IconAbout"></x:String>
将该字符代码更改为 󰋟
使一切正常 - 一个缺少零,结果是混乱。非常感谢@shaw 和@tommy99 - 试图实现他们的答案(但它仍然不起作用)让我找到了解决方案。我对他们的解决方案/评论表示感谢。