FreshMvvm icon indicating copy to clipboard operation
FreshMvvm copied to clipboard

Support for Font Icons

Open donniefitz2 opened this issue 5 years ago • 4 comments

Now that Xamarin supports font icons, would it be possible to support font icons in the tabbed navigation container? So, instead of passing an icon name, I'd like to be able to pass a font icon name for the tab icons. Looking for guidance on this.

donniefitz2 avatar May 08 '20 19:05 donniefitz2

We can add this as a feature but if you need it now then you can inherit the tabbed container.

rid00z avatar May 08 '20 22:05 rid00z

Thanks for the quick reply. I'll dig into creating my own tabbed container. If all goes well, I'll submit a PR.

donniefitz2 avatar May 08 '20 22:05 donniefitz2

using System.Collections.Generic;
using FreshMvvm;
using Xamarin.Forms;

namespace RmedAgent3
{
    //
    public class MyTabbedNavigationContainer: FreshTabbedNavigationContainer
    {
        public Page AddTab<T>(string title, ImageSource imageSource, object data = null) where T : FreshBasePageModel
        {
            Page page = FreshPageModelResolver.ResolvePageModel<T>(data);
            page.GetModel().CurrentNavigationServiceName = NavigationServiceName;

            var tabs = TabbedPages as List<Page>;
            tabs.Add(page);
            Page page2 = MyCreateContainerPageSafe(page);
            page2.Title = title;
            if (imageSource != null && !imageSource.IsEmpty)
            {
                //page2.Icon = icon;
                page2.IconImageSource = imageSource;
            }
            base.Children.Add(page2);
            return page2;
        }

        public Page MyCreateContainerPageSafe(Page page)
        {
            if (page is NavigationPage || page is MasterDetailPage || page is TabbedPage)
            {
                return page;
            }

            return CreateContainerPage(page);
        }
    }
}

usage:

public App()
{
    //RegisterSampleServices();
    RegisterServices();

    InitializeComponent();

    //MainPage = new MainPage();
    /*
    var loginPage = FreshPageModelResolver.ResolvePageModel<LoginPageModel>();
    MainPage = loginPage;
    */

    var mainPage = new MyTabbedNavigationContainer();

    mainPage.AddTab<HomePageModel>("Главная"  , GetMaterialIconSource(MaterialIcons.Home));
    mainPage.AddTab<HomePageModel>("Задания", GetMaterialIconSource(MaterialIcons.Inbox));
    mainPage.AddTab<HomePageModel>("Заявки", GetMaterialIconSource(MaterialIcons.Assignment));
    mainPage.AddTab<HomePageModel>("Сканер", GetMaterialIconSource(MaterialIcons.CenterFocusStrong));
    mainPage.AddTab<HomePageModel>("Еще" , GetMaterialIconSource(MaterialIcons.MoreHoriz));
    

    mainPage.On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
    mainPage.BarTextColor = Color.White;
    mainPage.BarBackgroundColor = App.GetColor("BrandColor");
    mainPage.SelectedTabColor = Color.White;
    mainPage.UnselectedTabColor = App.GetColor("UnselectedTabColor");

    MainPage = mainPage;
}

public FontImageSource GetMaterialIconSource(string iconName)
{
    return new FontImageSource()
    {
        FontFamily = (string) Application.Current.Resources["MaterialFont"]
        , Glyph = iconName,
    };
}

result:

tabbedpage_bottom_font_icons

protokovich avatar Jun 17 '20 11:06 protokovich

@protokovich Thanks for posting this. I meant to do the same but had other priorities.

donniefitz2 avatar Jun 17 '20 17:06 donniefitz2