簡體   English   中英

獲取 Xamarin.Forms 上 TabbedPage 的原生 iOS 系統選項卡欄圖標

[英]Get native iOS system tab bar icons for TabbedPage on Xamarin.Forms

我是 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>

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

我為您創建了一個示例,您可以在此處找到。 如果您有任何問題,請詢問!

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM