Skip to content

Instantly share code, notes, and snippets.

@ChaseFlorell
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ChaseFlorell/de3d43217e139c5b88fe to your computer and use it in GitHub Desktop.
Save ChaseFlorell/de3d43217e139c5b88fe to your computer and use it in GitHub Desktop.
This Gist is in response to the following Xamarin.Forms question: http://forums.xamarin.com/discussion/comment/68992/#Comment_68992
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace perfomance
{
public class App
{
public static INavigation Nav;
public static Page GetMainPage()
{
var root = new MyRootPage();
var navWrapper = new NavigationPage(root);
Nav = navWrapper.Navigation;
return navWrapper;
}
}
public class MyRootPage : ContentPage
{
private readonly Button _button;
public MyRootPage()
{
_button = new Button {Text = "Click me!", HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.StartAndExpand};
Content = _button;
}
protected override void OnAppearing()
{
base.OnAppearing();
_button.Clicked += async (s, e) =>
{
var cp = new MyCarouselPage();
await App.Nav.PushAsync(cp);
};
}
}
public class MyCarouselPage : CarouselPage
{
public MyCarouselPage()
{
var page1 = CreateACarouselSection();
Children.Add(page1);
var page2 = CreateACarouselSection();
Children.Add(page2);
}
private static MyCarouselSection CreateACarouselSection()
{
var dvm1 = new DataViewModel();
return new MyCarouselSection(dvm1);
}
}
public class MyCarouselSection : ContentPage
{
private readonly DataViewModel _dataViewModel;
public MyCarouselSection(DataViewModel dataViewModel)
{
_dataViewModel = dataViewModel;
BindingContext = _dataViewModel;
var dataTemplate = new DataTemplate(typeof (MyListCellTemplate));
dataTemplate.CreateContent();
var list = new ListView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
ItemTemplate = dataTemplate
};
var header = new Label {Text = "Header", VerticalOptions = LayoutOptions.Start, HorizontalOptions = LayoutOptions.FillAndExpand};
var footer = new Label {Text = "Footer", VerticalOptions = LayoutOptions.End, HorizontalOptions = LayoutOptions.FillAndExpand};
list.SetBinding(ListView.ItemsSourceProperty, "Data");
Content = new StackLayout {Orientation = StackOrientation.Vertical, Padding = 10, Children = {header, list, footer}};
}
protected override async void OnAppearing()
{
base.OnAppearing();
for (var i = 0; i < 50; i++)
{
// awaiting this allows the list item to build one at a time == WIN!
await Task.Run(() => _dataViewModel.Data.Add(new Data()));
}
}
}
public class DataViewModel
{
private IList<Data> _data;
public DataViewModel()
{
Data = new List<Data>();
}
public IList<Data> Data
{
get { return _data; }
set
{
_data = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class MyListCellTemplate : ViewCell
{
public MyListCellTemplate()
{
var ico = new Image {HorizontalOptions = LayoutOptions.Start, VerticalOptions = LayoutOptions.FillAndExpand};
var val1 = new Label
{
Font = Font.SystemFontOfSize(NamedSize.Small, FontAttributes.Bold | FontAttributes.Italic),
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand
};
var val2 = new Label
{
Font = Font.SystemFontOfSize(NamedSize.Micro),
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand
};
var val3 = new Label {HorizontalOptions = LayoutOptions.End, VerticalOptions = LayoutOptions.FillAndExpand};
ico.SetBinding(Image.SourceProperty, "Ico", BindingMode.OneWay, new IconConverter());
val1.SetBinding(Label.TextProperty, "Val1", BindingMode.OneWay);
val2.SetBinding(Label.TextProperty, "Val2", BindingMode.OneWay);
val3.SetBinding(Label.TextProperty, "Val3", BindingMode.OneWay);
var innerLayout = new StackLayout
{
Orientation = StackOrientation.Vertical,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children =
{
val1,
val2
}
};
View = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Children =
{
ico,
innerLayout,
val3
}
};
}
}
public class IconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ImageSource.FromResource(value.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
public class Data
{
public Data()
{
Ico = "perfomance.Resources.img.png";
Val1 = "Value 1";
Val2 = "Value 2";
Val3 = "100";
}
public string Ico { get; set; }
public string Val1 { get; set; }
public string Val2 { get; set; }
public string Val3 { get; set; }
}
}
@ChaseFlorell
Copy link
Author

I can confirm the exception

08-06 10:00:04.329 E/mono    (24071): Unhandled Exception:
08-06 10:00:04.329 E/mono    (24071): System.ArgumentException: 'jobject' must not be IntPtr.Zero.
08-06 10:00:04.329 E/mono    (24071): Parameter name: jobject
08-06 10:00:04.329 E/mono    (24071):   at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/a5d57087/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:169 
08-06 10:00:04.329 E/mono    (24071):   at Android.Widget.ImageView.get_Drawable () [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/a5d57087/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.ImageView.cs:511 
08-06 10:00:04.329 E/mono    (24071):   at Xamarin.Forms.Platform.Android.ImageRenderer.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
The program 'Mono' has exited with code 0 (0x0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment