Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ImageListView for Xamarin.Forms
using MoveNavi.Models;
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace MyTwitterApp.Views
{
public class TweetImages : ContentView
{
#region ItemsSource BindableProperty
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create(
nameof(ItemsSource), // 対象のプロパティ名 (文字列)
typeof(List<ArticleMedia>), // 対象のプロパティの型
typeof(TweetImages), // プロパティを定義する型(自クラス)
null, // プロパティのデフォルト値
propertyChanged: (bindable, oldValue, newValue) =>
{ // 変更通知ハンドラ
((TweetImages)bindable).ItemsSource = (List<ArticleMedia>)newValue;
},
defaultBindingMode: BindingMode.TwoWay // デフォルトのバインディングモード
);
public List<ArticleMedia> ItemsSource
{
get { return (List<ArticleMedia>)GetValue(ItemsSourceProperty); }
set
{
SetValue(ItemsSourceProperty, value);
// 値の変更をViewに反映(多分イベントでやるべき)
var stack = this.Content as StackLayout;
stack.Children.Clear();
if (value != null)
{
foreach (var item in value)
{
stack.Children.Add(new Image()
{
Source = item.Url,
HeightRequest = 100,
});
}
}
}
}
#endregion
public TweetImages()
{
Content = new StackLayout() {
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.Center,
Children = { }
};
SizeChanged += OnSizeChanged;
}
private void OnSizeChanged(object sender, EventArgs e)
{
var stack = this.Content as StackLayout;
foreach (var item in stack.Children)
{
item.WidthRequest = Width * 0.9 / stack.Children.Count;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment