iOS renderers now properly look up the ImageSourceHandler for FileImageSource (#826)

This commit is contained in:
James Clancey 2017-03-21 07:02:39 -08:00 коммит произвёл Rui Marinho
Родитель 1bb7796951
Коммит 4e9d99fea4
4 изменённых файлов: 37 добавлений и 18 удалений

Просмотреть файл

@ -26,7 +26,7 @@ namespace Xamarin.Forms.Platform.iOS
_forceName = forceName; _forceName = forceName;
_item = item; _item = item;
if (!string.IsNullOrEmpty(item.Icon) && !forceName) if (!string.IsNullOrEmpty(item.Icon?.File) && !forceName)
UpdateIconAndStyle(); UpdateIconAndStyle();
else else
UpdateTextAndStyle(); UpdateTextAndStyle();
@ -52,14 +52,14 @@ namespace Xamarin.Forms.Platform.iOS
UpdateIsEnabled(); UpdateIsEnabled();
else if (e.PropertyName == MenuItem.TextProperty.PropertyName) else if (e.PropertyName == MenuItem.TextProperty.PropertyName)
{ {
if (string.IsNullOrEmpty(_item.Icon) || _forceName) if (string.IsNullOrEmpty(_item.Icon?.File) || _forceName)
UpdateTextAndStyle(); UpdateTextAndStyle();
} }
else if (e.PropertyName == MenuItem.IconProperty.PropertyName) else if (e.PropertyName == MenuItem.IconProperty.PropertyName)
{ {
if (!_forceName) if (!_forceName)
{ {
if (!string.IsNullOrEmpty(_item.Icon)) if (!string.IsNullOrEmpty(_item.Icon?.File))
UpdateIconAndStyle(); UpdateIconAndStyle();
else else
UpdateTextAndStyle(); 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<IImageSourceHandler>(_item.Icon.GetType());
var image = await source.LoadImageAsync(_item.Icon);
Image = image; Image = image;
Style = UIBarButtonItemStyle.Plain; Style = UIBarButtonItemStyle.Plain;
} }
@ -123,9 +124,15 @@ namespace Xamarin.Forms.Platform.iOS
UpdateIsEnabled(); 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<IImageSourceHandler>(_item.Icon.GetType());
image = await source.LoadImageAsync(_item.Icon);
}
((SecondaryToolbarItemContent)CustomView).Image = image;
} }
void UpdateIsEnabled() void UpdateIsEnabled()

Просмотреть файл

@ -120,7 +120,7 @@ namespace Xamarin.Forms.Platform.iOS
if (target != null) if (target != null)
{ {
cell.Name = target.Title; cell.Name = target.Title;
cell.Icon = target.Icon; cell.Icon = target.Icon?.File;
cell.Selected = () => MenuController.SendTargetSelected(target); cell.Selected = () => MenuController.SendTargetSelected(target);
} }
else else

Просмотреть файл

@ -349,12 +349,11 @@ namespace Xamarin.Forms.Platform.iOS
//var pack = Platform.GetRenderer (view).ViewController; //var pack = Platform.GetRenderer (view).ViewController;
var titleIcon = NavigationPage.GetTitleIcon(page); var titleIcon = NavigationPage.GetTitleIcon(page);
if (!string.IsNullOrEmpty(titleIcon)) if (!string.IsNullOrEmpty(titleIcon?.File))
{ {
try try
{ {
//UIImage ctor throws on file not found if MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure is true; setTitleImage(pack,titleIcon);
pack.NavigationItem.TitleView = new UIImageView(new UIImage(titleIcon));
} }
catch catch
{ {
@ -376,6 +375,14 @@ namespace Xamarin.Forms.Platform.iOS
return pack; return pack;
} }
async void setTitleImage(ParentingViewController pack, FileImageSource titleIcon)
{
var source = Internals.Registrar.Registered.GetHandler<IImageSourceHandler>(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() void FindParentMasterDetail()
{ {
var parentPages = ((Page)Element).GetParentPages(); 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()) if (!masterDetailPage.ShouldShowToolbarButton())
{ {
@ -667,7 +674,10 @@ namespace Xamarin.Forms.Platform.iOS
{ {
try try
{ {
containerController.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(new UIImage(masterDetailPage.Master.Icon), UIBarButtonItemStyle.Plain, handler);
var source = Internals.Registrar.Registered.GetHandler<IImageSourceHandler>(masterDetailPage.Master.Icon.GetType());
var icon = await source.LoadImageAsync(masterDetailPage.Master.Icon);
containerController.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(icon, UIBarButtonItemStyle.Plain, handler);
} }
catch (Exception) catch (Exception)
{ {

Просмотреть файл

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.ComponentModel; using System.ComponentModel;
using System.Threading.Tasks;
using UIKit; using UIKit;
using Xamarin.Forms.Internals; using Xamarin.Forms.Internals;
using static Xamarin.Forms.PlatformConfiguration.iOSSpecific.Page; using static Xamarin.Forms.PlatformConfiguration.iOSSpecific.Page;
@ -385,13 +386,13 @@ namespace Xamarin.Forms.Platform.iOS
VisualElementRenderer<VisualElement>.RegisterEffect(effect, View); VisualElementRenderer<VisualElement>.RegisterEffect(effect, View);
} }
void SetTabBarItem(IVisualElementRenderer renderer) async void SetTabBarItem(IVisualElementRenderer renderer)
{ {
var page = renderer.Element as Page; var page = renderer.Element as Page;
if(page == null) if(page == null)
throw new InvalidCastException($"{nameof(renderer)} must be a {nameof(Page)} renderer."); 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) renderer.ViewController.TabBarItem = new UITabBarItem(page.Title, icons?.Item1, icons?.Item2)
{ {
Tag = Tabbed.Children.IndexOf(page), 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), /// 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. /// or null if no icon should be set.
/// </returns> /// </returns>
protected virtual Tuple<UIImage, UIImage> GetIcon(Page page) protected virtual async Task<Tuple<UIImage, UIImage>> 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<IImageSourceHandler>(page.Icon.GetType());
var icon = await source.LoadImageAsync(page.Icon);
return Tuple.Create(icon, (UIImage)null); return Tuple.Create(icon, (UIImage)null);
} }