gpt4 book ai didi

xamarin - 可以在 Xamarin Forms 的选项卡上使用系统图标吗?

转载 作者:行者123 更新时间:2023-12-04 05:39:23 25 4
gpt4 key购买 nike

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

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

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

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

这是我当前的代码:

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} };

最佳答案

iOS版

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

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
}
}

enter image description here

安卓版

事情更容易

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;
}
}
}

}

enter image description here

关于xamarin - 可以在 Xamarin Forms 的选项卡上使用系统图标吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29560305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com