Skip to content

Instantly share code, notes, and snippets.

@dbeattie71
Forked from ChaseFlorell/App.cs
Created August 7, 2014 16:42
Show Gist options
  • Save dbeattie71/318067c6f3c6379ccf9a to your computer and use it in GitHub Desktop.
Save dbeattie71/318067c6f3c6379ccf9a to your computer and use it in GitHub Desktop.
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; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment