Skip to content

Instantly share code, notes, and snippets.

@dedene
Forked from georgejecook/FastCell.cs
Last active August 29, 2015 14:20
Show Gist options
  • Save dedene/401a8f24774079b963bc to your computer and use it in GitHub Desktop.
Save dedene/401a8f24774079b963bc to your computer and use it in GitHub Desktop.
using System;
using Xamarin.Forms;
namespace TwinEvents.Core.Media.View
{
public class FastCell : ViewCell
{
}
}
using System;
using Xamarin.Forms.Platform.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using TwinEvents.Ios.Media;
using TwinEvents.Core.Media.View;
[assembly: ExportRenderer (typeof(FastCell), typeof(FastCellRenderer))]
namespace TwinEvents.Ios.Media
{
internal class NativeCell : UITableViewCell
{
UIView _view;
public ViewCell XamarinView {
get;
set;
}
bool _isViewLaidOut;
public NativeCell (NSString cellId, ViewCell view) : base (UITableViewCellStyle.Default, cellId)
{
XamarinView = view;
var renderer = RendererFactory.GetRenderer (view.View);
_view = renderer.NativeView;
ContentView.AddSubview (_view);
}
public override void LayoutSubviews ()
{
base.LayoutSubviews ();
//TODO update sizes of the xamarin view
if (!_isViewLaidOut) {
var layout = XamarinView.View as AbsoluteLayout;
layout.Layout (Frame.ToRectangle ());
layout.ForceLayout ();
_isViewLaidOut = true;
}
_view.Frame = ContentView.Bounds;
}
}
public class FastCellRenderer : ViewCellRenderer
{
NSString cellId = new NSString ("Cell");
public override UITableViewCell GetCell (Cell item, UITableViewCell reusableCell, UITableView tv)
{
var nativeCell = tv.DequeueReusableCell (cellId) as NativeCell;
var viewCell = item as ViewCell;
if (nativeCell == null) {
nativeCell = new NativeCell (cellId, viewCell);
} else {
nativeCell.XamarinView.BindingContext = item.BindingContext;
}
return nativeCell;
}
}
}
using System;
using Xamarin.Forms;
using System.Diagnostics;
namespace TwinEvents.Core.Media.View
{
public interface IFastImageProvider
{
void SetImageUrl (string imageUrl);
}
public class FastImage : Image
{
public static readonly BindableProperty ImageUrlProperty = BindableProperty.Create<FastImage, string> (w => w.ImageUrl, null);
/// <summary>
/// sets the image URL.
/// </summary>
/// <value>The image URL.</value>
public string ImageUrl {
get { return (string)GetValue (ImageUrlProperty); }
set {
SetValue (ImageUrlProperty, value);
}
}
}
}
using System;
using Xamarin.Forms.Platform.iOS;
using XLabs.Platform.Device;
using UIKit;
using SDWebImage;
using Foundation;
using TwinEvents.Core.Media.View;
using Xamarin.Forms;
using TwinEvents.Ios.Media;
[assembly: ExportRenderer (typeof(FastImage), typeof(FastImageRenderer))]
namespace TwinEvents.Ios.Media
{
public class FastImageRenderer : ImageRenderer, IFastImageProvider
{
public FastImageRenderer ()
{
}
protected override void OnElementChanged (ElementChangedEventArgs<Image> e)
{
base.OnElementChanged (e);
// if (e.OldElement != null) {
// ((FastImage)e.OldElement).ImageProvider = null;
// }
if (e.NewElement != null) {
var fastImage = e.NewElement as FastImage;
SetImageUrl (fastImage.ImageUrl);
}
}
protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged (sender, e);
if (e.PropertyName == "ImageUrl") {
var fastImage = Element as FastImage;
SetImageUrl (fastImage.ImageUrl);
}
}
#region FastImageProvider implementation
public void SetImageUrl (string imageUrl)
{
if (Control == null) {
return;
}
if (imageUrl != null) {
Control.SetImage (
url: new NSUrl (imageUrl),
placeholder: UIImage.FromBundle ("placeholder.png")
);
} else {
Control.Image = UIImage.FromBundle ("placeholder.png");
}
}
#endregion
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- example cell -->
<local:FastCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TwinEvents.Core.Media.View.MediaCellXaml"
xmlns:local="clr-namespace:TwinEvents.Core.Media.View;assembly=TwinEvents"
>
<AbsoluteLayout BackgroundColor="Black">
<local:FastImage x:Name="ImageView" AbsoluteLayout.LayoutFlags="None" AbsoluteLayout.LayoutBounds="0,0,320,260"/>
<local:FastImage x:Name="UserThumbnailView" AbsoluteLayout.LayoutFlags="None" AbsoluteLayout.LayoutBounds="5,240,40,40"/>
<Label x:Name="NameLabel" Text="some text" TextColor="White" AbsoluteLayout.LayoutFlags="None" AbsoluteLayout.LayoutBounds="50,250,100,20"/>
<Label x:Name="DescriptionLabel" Text="some text" TextColor="White" AbsoluteLayout.LayoutFlags="None" AbsoluteLayout.LayoutBounds="50,270,100,40"/>
</AbsoluteLayout>
</local:FastCell>
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using TwinEvents.Core.Media.Model;
//Example cell code behind
namespace TwinEvents.Core.Media.View
{
public partial class MediaCellXaml : FastCell
{
public MediaCellXaml ()
{
InitializeComponent ();
}
protected override void OnBindingContextChanged ()
{
base.OnBindingContextChanged ();
var mediaItem = BindingContext as IMediaItem;
if (mediaItem != null) {
// UserThumbnailView.Source = mediaItem.UserThumbnailImageSource;
// ImageView.Source = mediaItem.ImageSource;
UserThumbnailView.ImageUrl = mediaItem.UserThumbnailPath != null ? mediaItem.UserThumbnailPath.AbsoluteUri : null;
ImageView.ImageUrl = mediaItem.UserThumbnailPath != null ? mediaItem.ImagePath.AbsoluteUri : null;
// NameLabel.Text = mediaItem.Name;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment