Vay*_*age 1 silverlight xaml windows-phone-7
我有一个图像URL的数组.我想为每个图像Url动态添加Pivot项目,并为每个轴项目添加一个图像框以显示图像.如何继续?请帮忙.
感谢致敬
vaysage
我找到了解决方案.在数组(arrayFeed)中创建所有图像URL并执行以下操作.
for (int i = 0; i < arrayFeed.Count - 1; i++)
{
PivotItem pivotItem = new PivotItem();
pivotItem.Header = i.ToString();
Grid grid = new Grid();
Image img = new Image();
img.Source = new BitmapImage(new Uri((arrayFeed[i]));
grid.Children.Add(img);
pivotItem.Content = grid;
mainPivot.Items.Add(pivotItem);
}
Run Code Online (Sandbox Code Playgroud)
这不是推荐的方法.请改用数据绑定.
制作arrayFeed类型ObservableCollection<Uri>,分配ItemsSource,mainPivot并用于ItemTemplate自定义您的项目UI.
码:
ObservableCollection<Uri> arrayFeed = new ObservableCollection<Uri>();
// populate arrayField
mainPivot.ItemsSource = arrayFeed;
Run Code Online (Sandbox Code Playgroud)
XAML:
<Pivot Name="mainPivot">
<Pivot.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" />
</DataTemplate>
</Pivot.ItemTemplate>
</Pivot>
Run Code Online (Sandbox Code Playgroud)
- 编辑 -
对于你的评论,
通常,ObservableCollection在Silverlight/中使用数据绑定时是个好主意WPF.ObservableCollection实现INotifyCollectionChanged接口.UI每当添加/删除项目时通知元素都是有帮助的ObservableCollection.这种方式UI可以自我更新.