获取Xamarin.Forms上TabbedPage的原生iOS系统选项卡栏图标



我是Xamarin.Forms和XAML的新手。我正试图让选项卡图标只为IOS显示在我的TabbedPage中的不同页面。我做了一些搜索,我知道UIKit引用了UITabBarSystem上IOS上的系统图标。我如何使用枚举中的元素而不必从互联网上获取这些图标?TabbedPage是具有子页面的根,这些子页面是ContentPages和ListView页面。因此,正如你将从所附样品中看到的,我想要";IconImageSource";FavoritesPage的属性,以便从iOS UIKit获取图像。这可能吗?

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:PhoneApp.Views"
x:Class="PhoneApp.Views.MainPage">
<TabbedPage.Title>

</TabbedPage.Title>
<TabbedPage.Children>
<views:FavouritesPage Title="Favourites" IconImageSource=""/>
<views:RecentsPage Title="Recents"/>
<views:ContactsPage Title="Contacts"/>
<views:KeypadPage Title="Keypad"/>
<views:VoicemailPage Title="Voicemail"/>
</TabbedPage.Children>
</TabbedPage>

我想我找到了适合你的解决方案。如果你想在Xamarin控件上使用原生Api,你可以为它们使用自定义渲染器,这很好!以下是TabedPage的渲染器:

[assembly: ExportRenderer(typeof(MainPage), typeof(MyTabbedPageRenderer))]
namespace TestApp.iOS
{
public class MyTabbedPageRenderer : TabbedRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (TabBar?.Items == null) return;
//Setting the Icons
TabBar.Items[0].Image = GetTabIcon(UITabBarSystemItem.Search);
TabBar.Items[1].Image = GetTabIcon(UITabBarSystemItem.Downloads);
TabBar.Items[2].Image = GetTabIcon(UITabBarSystemItem.Bookmarks);
}
private UIImage GetTabIcon(UITabBarSystemItem systemItem)
{
//Convert UITabBarItem to UIImage
UITabBarItem item = new UITabBarItem(systemItem, 0);
return UIImage.FromImage(item.SelectedImage.CGImage, item.SelectedImage.CurrentScale, item.SelectedImage.Orientation);
}
}
}

我为你创建了一个样本,你可以在这里找到。如果你有任何问题,请提问!

问候

最新更新