Created
March 22, 2017 08:11
-
-
Save m2wasabi/30ce84b58485164179799359b90bed5f to your computer and use it in GitHub Desktop.
ImageListView for Xamarin.Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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