我需要在LongListMultiSelector中显示我的设备(WP8)中的所有照片。 我用这个方法
MediaPlayer.Queue.ToString();
MediaLibrary mediaLibrary;
PictureAlbum cameraRoll = null;
foreach (MediaSource source in MediaSource.GetAvailableMediaSources())
{
if (source.MediaSourceType == MediaSourceType.LocalDevice)
{
mediaLibrary = new MediaLibrary(source);
PictureAlbumCollection allAlbums = mediaLibrary.RootPictureAlbum.Albums;
foreach (PictureAlbum album in allAlbums)
{
if (album.Name == "Camera Roll")
{
cameraRoll = album;
}
}
}
}
List<BitmapImage> lstBitmapImage = new List<BitmapImage>();
foreach (Picture p in cameraRoll.Pictures)
{
BitmapImage b = new BitmapImage();
b.SetSource(p.GetThumbnail());
lstBitmapImage.Add(b);
}
PhotoHubLLS.ItemsSource = lstBitmapImage;
在XAML中我有这个图像设置
<Image HorizontalAlignment="Left" Margin="6,6,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Source="{Binding}"/>
这一切都很完美,但我有一些问题。
我想放大单张图片,在图片点按我插入此代码
FrameworkElement fe = sender as FrameworkElement;
if (fe != null)
{
CurrentPicture = fe.DataContext as Picture;
}
但是null为datacontext,因为我使用了“Source”。
我该怎么办?
答案 0 :(得分:0)
这取决于您连接的事件。如果您正在处理SelectionChanged事件,则可以从SelectionChangedEventArgs事件参数参数中的AddedItems集合中检索BitmapImage(而不是Picture):
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
BitmapImage bmp = e.AddedItems[0] as BitmapImage;
}
}
或者,如果您正在处理LongListSelector的ItemTemplate中的Image元素的Tap事件,那么您可以从sender参数中检索BitmapImage:
Image imgElement = sender as Image;
BitmapImage bmp = imgElement.Source as BitmapImage;