From 4e9d99fea4abc7c8c1a29ab2fbfa704cab458d72 Mon Sep 17 00:00:00 2001 From: James Clancey Date: Tue, 21 Mar 2017 07:02:39 -0800 Subject: iOS renderers now properly look up the ImageSourceHandler for FileImageSource (#826) --- .../Extensions/ToolbarItemExtensions.cs | 21 ++++++++++++++------- .../Renderers/NavigationMenuRenderer.cs | 2 +- .../Renderers/NavigationRenderer.cs | 20 +++++++++++++++----- .../Renderers/TabbedRenderer.cs | 12 +++++++----- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/Xamarin.Forms.Platform.iOS/Extensions/ToolbarItemExtensions.cs b/Xamarin.Forms.Platform.iOS/Extensions/ToolbarItemExtensions.cs index 295ebd52..84cea868 100644 --- a/Xamarin.Forms.Platform.iOS/Extensions/ToolbarItemExtensions.cs +++ b/Xamarin.Forms.Platform.iOS/Extensions/ToolbarItemExtensions.cs @@ -26,7 +26,7 @@ namespace Xamarin.Forms.Platform.iOS _forceName = forceName; _item = item; - if (!string.IsNullOrEmpty(item.Icon) && !forceName) + if (!string.IsNullOrEmpty(item.Icon?.File) && !forceName) UpdateIconAndStyle(); else UpdateTextAndStyle(); @@ -52,14 +52,14 @@ namespace Xamarin.Forms.Platform.iOS UpdateIsEnabled(); else if (e.PropertyName == MenuItem.TextProperty.PropertyName) { - if (string.IsNullOrEmpty(_item.Icon) || _forceName) + if (string.IsNullOrEmpty(_item.Icon?.File) || _forceName) UpdateTextAndStyle(); } else if (e.PropertyName == MenuItem.IconProperty.PropertyName) { if (!_forceName) { - if (!string.IsNullOrEmpty(_item.Icon)) + if (!string.IsNullOrEmpty(_item.Icon?.File)) UpdateIconAndStyle(); else UpdateTextAndStyle(); @@ -67,9 +67,10 @@ namespace Xamarin.Forms.Platform.iOS } } - void UpdateIconAndStyle() + async void UpdateIconAndStyle() { - var image = UIImage.FromBundle(_item.Icon); + var source = Internals.Registrar.Registered.GetHandler(_item.Icon.GetType()); + var image = await source.LoadImageAsync(_item.Icon); Image = image; Style = UIBarButtonItemStyle.Plain; } @@ -123,9 +124,15 @@ namespace Xamarin.Forms.Platform.iOS UpdateIsEnabled(); } - void UpdateIcon() + async void UpdateIcon() { - ((SecondaryToolbarItemContent)CustomView).Image = string.IsNullOrEmpty(_item.Icon) ? null : new UIImage(_item.Icon); + UIImage image = null; + if (!string.IsNullOrEmpty(_item.Icon?.File)) + { + var source = Internals.Registrar.Registered.GetHandler(_item.Icon.GetType()); + image = await source.LoadImageAsync(_item.Icon); + } + ((SecondaryToolbarItemContent)CustomView).Image = image; } void UpdateIsEnabled() diff --git a/Xamarin.Forms.Platform.iOS/Renderers/NavigationMenuRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/NavigationMenuRenderer.cs index 11a8558f..af823a4d 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/NavigationMenuRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/NavigationMenuRenderer.cs @@ -120,7 +120,7 @@ namespace Xamarin.Forms.Platform.iOS if (target != null) { cell.Name = target.Title; - cell.Icon = target.Icon; + cell.Icon = target.Icon?.File; cell.Selected = () => MenuController.SendTargetSelected(target); } else diff --git a/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs index e054a2d3..2ca9611e 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs @@ -349,12 +349,11 @@ namespace Xamarin.Forms.Platform.iOS //var pack = Platform.GetRenderer (view).ViewController; var titleIcon = NavigationPage.GetTitleIcon(page); - if (!string.IsNullOrEmpty(titleIcon)) + if (!string.IsNullOrEmpty(titleIcon?.File)) { try { - //UIImage ctor throws on file not found if MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure is true; - pack.NavigationItem.TitleView = new UIImageView(new UIImage(titleIcon)); + setTitleImage(pack,titleIcon); } catch { @@ -376,6 +375,14 @@ namespace Xamarin.Forms.Platform.iOS return pack; } + async void setTitleImage(ParentingViewController pack, FileImageSource titleIcon) + { + var source = Internals.Registrar.Registered.GetHandler(titleIcon.GetType()); + var image = await source.LoadImageAsync(titleIcon); + //UIImage ctor throws on file not found if MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure is true; + pack.NavigationItem.TitleView = new UIImageView(image); + } + void FindParentMasterDetail() { var parentPages = ((Page)Element).GetParentPages(); @@ -652,7 +659,7 @@ namespace Xamarin.Forms.Platform.iOS } } - internal static void SetMasterLeftBarButton(UIViewController containerController, MasterDetailPage masterDetailPage) + internal static async void SetMasterLeftBarButton(UIViewController containerController, MasterDetailPage masterDetailPage) { if (!masterDetailPage.ShouldShowToolbarButton()) { @@ -667,7 +674,10 @@ namespace Xamarin.Forms.Platform.iOS { try { - containerController.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(new UIImage(masterDetailPage.Master.Icon), UIBarButtonItemStyle.Plain, handler); + + var source = Internals.Registrar.Registered.GetHandler(masterDetailPage.Master.Icon.GetType()); + var icon = await source.LoadImageAsync(masterDetailPage.Master.Icon); + containerController.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(icon, UIBarButtonItemStyle.Plain, handler); } catch (Exception) { diff --git a/Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs index a1b2802a..8b943b6b 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; +using System.Threading.Tasks; using UIKit; using Xamarin.Forms.Internals; using static Xamarin.Forms.PlatformConfiguration.iOSSpecific.Page; @@ -385,13 +386,13 @@ namespace Xamarin.Forms.Platform.iOS VisualElementRenderer.RegisterEffect(effect, View); } - void SetTabBarItem(IVisualElementRenderer renderer) + async void SetTabBarItem(IVisualElementRenderer renderer) { var page = renderer.Element as Page; if(page == null) throw new InvalidCastException($"{nameof(renderer)} must be a {nameof(Page)} renderer."); - var icons = GetIcon(page); + var icons = await GetIcon(page); renderer.ViewController.TabBarItem = new UITabBarItem(page.Title, icons?.Item1, icons?.Item2) { Tag = Tabbed.Children.IndexOf(page), @@ -408,11 +409,12 @@ namespace Xamarin.Forms.Platform.iOS /// A tuple containing as item1: the unselected version of the icon, item2: the selected version of the icon (item2 can be null), /// or null if no icon should be set. /// - protected virtual Tuple GetIcon(Page page) + protected virtual async Task> GetIcon(Page page) { - if (!string.IsNullOrEmpty(page.Icon)) + if (!string.IsNullOrEmpty(page.Icon?.File)) { - var icon = new UIImage(page.Icon); + var source = Internals.Registrar.Registered.GetHandler(page.Icon.GetType()); + var icon = await source.LoadImageAsync(page.Icon); return Tuple.Create(icon, (UIImage)null); } -- cgit v1.2.3