【问题标题】:Get native iOS system tab bar icons for TabbedPage on Xamarin.Forms获取 Xamarin.Forms 上 TabbedPage 的本机 iOS 系统选项卡栏图标
【发布时间】:2021-04-14 23:44:06
【问题描述】:

我是 Xamarin.Forms 和 XAML 的新手。我正在尝试为我的 TabbedPage 中的不同页面仅显示 IOS 的选项卡图标。我做了一些搜索,发现 UIKit 在 UITabBarSystem 上引用了 IOS 上可用的系统图标。我怎样才能利用该枚举中的元素而不必从互联网上获取这些图标? TabbedPage 是具有子页面的根,这些子页面是 ContentPages 和 ListView 页面。因此,正如您从所附示例中看到的那样,我希望 FavouritesPage 的“IconImageSource”属性能够从 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>

【问题讨论】:

    标签: xaml xamarin.forms xamarin.ios uikit tabbedpage


    【解决方案1】:

    我想我找到了适合您的解决方案。如果您想在 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);
            }
        }
    }
    

    我为您创建了一个示例,您可以找到 here。如有任何问题,请追问!

    问候

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 2018-08-30
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2018-02-08
      相关资源
      最近更新 更多