可以在Xamarin表单的选项卡上使用系统图标吗?

可以在Xamarin表单的选项卡上使用系统图标吗?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,使用Xamarin表单时,是否有方法指定要在选项卡上显示的“系统”图标?我想使用收藏夹、书签、历史记录等图标,但我不想为各种平台提供我自己的所有图像 使用Xamarin.iOS可以使用以下语法: tab1.TabBarItem=new UITabBarItem(uitabbarsystemimitem.Favorites,0); 然而,我在跨平台的Xamarin.Forms项目中找不到如何做到这一点 这是我当前的代码: var profilePage=新内容页{ Title=“Profile”

使用Xamarin表单时,是否有方法指定要在选项卡上显示的“系统”图标?我想使用收藏夹、书签、历史记录等图标,但我不想为各种平台提供我自己的所有图像

使用Xamarin.iOS可以使用以下语法:

tab1.TabBarItem=new UITabBarItem(uitabbarsystemimitem.Favorites,0);
然而,我在跨平台的Xamarin.Forms项目中找不到如何做到这一点

这是我当前的代码:

var profilePage=新内容页{
Title=“Profile”,
//这需要将我自己的图像添加到项目中
//我希望使用特定于平台的内置图像
//收藏夹、书签、更多信息等。。。
//Icon=“Profile.png”,
内容=新的堆栈布局{
间距=20,填充=50,
垂直选项=布局选项。中心,
儿童={
新条目{Placeholder=“Username”},
新条目{Placeholder=“Password”,IsPassword=true},
新按钮{
Text=“登录”,
TextColor=Color.White,
BackgroundColor=Color.FromHex(“77D065”)};
var settingsPage=新内容页{
Title=“设置”,
内容=新的堆栈布局{
间距=20,填充=50,
垂直选项=布局选项。中心,
儿童={
新条目{Placeholder=“Username”},
新条目{Placeholder=“Password”,IsPassword=true}}
};
主页=新选项卡页{Children={profilePage,settingsPage};

您应该为iOS项目编写自定义选项卡页面呈现程序:

public类YourTabbedRenderer:TabbedRenderer{
公共覆盖无效视图将出现(布尔动画)
{
基本视图将显示(动画);
var items=TabBar.items;
对于(变量i=0;i

为了使用图标,您需要一个特定于平台的结构。在Xamarin.Forms中,要使其工作:

Icon = "youricon.png";
你需要:

  • iOS:将图像放入/Resources/yourcon.png
  • Android:将图像放入/Resources/drawable/yourcon.png
  • Win Phone:将映像放入Windows Phone应用程序项目根目录中

  • 适用于iOS的

您的页面需要自定义呈现程序。在我的示例中,它是CustomTabsPage类。不能仅使用系统图标创建UIImage。我们需要使用uitabaritem。问题是UITabBarItem不允许更改标题或图像/图标。但是,我们可以从中复制图像

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用UIKit;
使用ButtonUnderDemo;
使用ButtonRenderDemo.iOS;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.iOS;
[assembly:ExportRenderer(typeof(CustomTabsPage),typeof(CustomTabsPagerEnder))]
命名空间按钮UnderDemo.iOS
{
公共类CustomTabsPageRenderer:TabbedRenderer
{
#具有自定义标题的区域系统图像
公共覆盖无效视图将出现(布尔动画)
{
基本视图将显示(动画);
foreach(TabBar.Items中的变量项)
{
item.Image=GetTabIcon(item.Title);
}
}
私有UIImage GetTabIcon(字符串标题)
{
uitabaritem item=null;
开关(标题)
{
案例“简介”:
item=新的uitabaritem(uitabbasystemitem.Search,0);
打破
案例“设置”:
item=新的UITabBarItem(UITabBarSystemItem.Bookmarks,0);
打破
}
变量img=(item!=null)?UIImage.FromImage(item.SelectedImage.CGImage,item.SelectedImage.CurrentScale,item.SelectedImage.Orientation):新建UIImage();
返回img;
}
#端区
}
}

适用于Android

事情变得容易了

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用ButtonUnderDemo;
使用按钮描述机器人;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
使用Xamarin.Forms.Platform.Android.AppCompat;
使用Android.Support.Design.Widget;
[assembly:ExportRenderer(typeof(CustomTabsPage),typeof(CustomTabsPagerEnder))]
名称空间按钮UnderDemo.Droid
{
公共类CustomTabsPageRenderer:TabbedPageRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
//var layout=(TabLayout)ViewGroup.GetChildAt(1);//应该足够了,但为了健壮性,我们使用下面的循环
TabLayout布局=空;
for(int i=0;i

没有显示系统图像的方法吗