可以在 Xamarin Forms 的选项卡上使用系统图标吗? [英] Can one use system icons on tabs in Xamarin Forms?

查看:15
本文介绍了可以在 Xamarin Forms 的选项卡上使用系统图标吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Xamarin Forms 时,有没有办法指定要在选项卡上显示的系统"图标?我想使用收藏夹、书签、历史等图标,但我不想为各种平台提供我自己的所有图像.

Is there a way to specify a "system" icon to be displayed on a tab when using Xamarin Forms? I would like to use icons such as Favourites, Bookmark, History etc but I do not want to supply all my own images for the various platforms.

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

Using Xamarin.iOS one can use this syntax:

tab1.TabBarItem = new UITabBarItem (UITabBarSystemItem.Favorites, 0);

但是,我在跨平台 Xamarin.Forms 项目中找不到如何执行此操作.

I can however not find how to do this in the cross-platform Xamarin.Forms project.

这是我当前的代码:

var profilePage = new ContentPage {
    Title = "Profile",
    //This requires my own images to be added to the project whereas
    //I wish to use built-in images which are platform specific for
    //Favourites, Bookmark, More, etc...
    //Icon = "Profile.png",
    Content = new StackLayout {
        Spacing = 20, Padding = 50,
        VerticalOptions = LayoutOptions.Center,
        Children = {
        new Entry { Placeholder = "Username" },
        new Entry { Placeholder = "Password", IsPassword = true },
        new Button {
            Text = "Login",
            TextColor = Color.White,
            BackgroundColor = Color.FromHex("77D065") }}}};

var settingsPage = new ContentPage {
    Title = "Settings",
    Content = new StackLayout {
        Spacing = 20, Padding = 50,
        VerticalOptions = LayoutOptions.Center,
        Children = {
            new Entry { Placeholder = "Username" },
            new Entry { Placeholder = "Password", IsPassword = true }}}
        };


MainPage = new TabbedPage { Children = {profilePage, settingsPage} };

推荐答案

For iOS

您的页面需要一个自定义渲染器.在我的示例中,它是 CustomTabsPage 类.您不能只使用系统图标来创建 UIImage.我们需要使用 UITabBarItem.问题是 UITabBarItem 不允许更改标题或图像/图标.但是,我们可以从中复制图像.

you need a custom renderer for your page. In my example, it is CustomTabsPage class. You cannot just use system icons to create a UIImage. We need to use UITabBarItem. The problem is that UITabBarItem doesn't allow changes to either title or image/icon. But, we can copy an image from it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using UIKit;
using ButtonRendererDemo;
using ButtonRendererDemo.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(CustomTabsPage), typeof(CustomTabsPageRenderer))]
namespace ButtonRendererDemo.iOS
{
    public class CustomTabsPageRenderer : TabbedRenderer
    {

         #region Sytem Image with custom title

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            foreach (var item in TabBar.Items)
            {
                item.Image = GetTabIcon(item.Title);
            }
        }

        private UIImage GetTabIcon(string title)
        {
            UITabBarItem item = null;

            switch (title)
            {
                case "Profile":
                    item = new UITabBarItem(UITabBarSystemItem.Search, 0);
                    break;
                case "Settings":
                    item = new UITabBarItem(UITabBarSystemItem.Bookmarks, 0);
                    break;
            }

            var img = (item != null) ? UIImage.FromImage(item.SelectedImage.CGImage, item.SelectedImage.CurrentScale, item.SelectedImage.Orientation) : new UIImage();
            return img;
        }

        #endregion
    }
}

安卓版

事情更简单了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ButtonRendererDemo;
using ButtonRendererDemo.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
using Android.Support.Design.Widget;

[assembly: ExportRenderer(typeof(CustomTabsPage), typeof(CustomTabsPageRenderer))]
namespace ButtonRendererDemo.Droid
{
    public class CustomTabsPageRenderer : TabbedPageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);

            //var layout = (TabLayout)ViewGroup.GetChildAt(1); //should be enough but just for robustness we use loop below

            TabLayout layout = null;
            for (int i = 0; i < ChildCount; i++)
            {
                layout = GetChildAt(i) as TabLayout;
                if (layout != null)
                    break;
            }
            if (layout != null)
            {
                for (int tabIndex = 0; tabIndex < layout.TabCount; tabIndex++)
                    SetTabIcon(layout, tabIndex);
            }
        }

        private void SetTabIcon(TabLayout layout, int tabIndex)
        {
            var tab = layout.GetTabAt(tabIndex);

            switch (tabIndex)
            {
                case 0:
                    tab.SetIcon(Resource.Drawable.icon2);//from local resource
                    break;
                case 1:
                    tab.SetIcon(Resource.Drawable.ic_media_play_dark);//from Android system, depends on version !
                    break;
            }
        }
    }

}

这篇关于可以在 Xamarin Forms 的选项卡上使用系统图标吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆