[英]Windows Phone 8.1 ListBox ItemTemplate with a C# method for each item
目前,我在静态页面上使用以下代码在页面中加载图像。
图像存储在隔离的存储中,并且在数据库中存储图像的位置。
该图像已成功从隔离存储中读取,并显示在我的静态页面上。
我现在想做的是在ListBox
使用相同的图像。 但是如何通过DataTemplate
将相同的代码过程绑定到每个ListBox
项目?
这是在我的静态页面上运行的代码。
private void ReadFromIsolatedStorage(string _filename)
{
if (_filename != null)
{
WriteableBitmap bitmap = new WriteableBitmap(200, 200);
using (IsolatedStorageFile myIsoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsoStorage.OpenFile(_filename, FileMode.Open, FileAccess.Read))
{
//StreamResourceInfo sri = Application.GetResourceStream(fileStream);
bitmap = PictureDecoder.DecodeJpeg(fileStream);
BinaryReader binary = new BinaryReader(fileStream);
byte[] imgByteArray = binary.ReadBytes((int)(fileStream.Length));
binary.Close();
binary.Dispose();
_imagearray = imgByteArray;
}
}
this.imagebox1.Source = bitmap;
//StreamResourceInfo sri = bitmap;
}
}
这是ListBox
的不完整xaml,我需要以某种方式链接到上面的代码。
<ListBox Name="List1" Height="463" SelectionChanged="List1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="95" />
<ColumnDefinition Width="133" />
<ColumnDefinition Width="129" />
<ColumnDefinition Width="63" />
</Grid.ColumnDefinitions>
<Image Height="60" Width="60" Stretch="Uniform" Name="imageprofile" />
<TextBlock Grid.Column="1" Text="{Binding Type}" TextWrapping="Wrap"/>
<TextBlock Grid.Column="2" Text="{Binding One}" TextWrapping="Wrap"/>
<TextBlock Grid.Column="3" Text="{Binding two}" />
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您需要一个IValueConverter
您的页面静态资源:
<Page.Resources>
<converters:FilenameToBitmapImageConverter x:Key="FilenameToBitmapImageConverter"/>
</Page.Resources>
在您的项目模板中:
<Image Source="{Binding FileName,Converter={StaticResource FilenameToBitmapImageConverter}}"/>
您的转换器代码:
public class FilenameToBitmapImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string fileName = value as string;
if (fileName != null)
{
WriteableBitmap bitmap = new WriteableBitmap(200, 200);
using (IsolatedStorageFile myIsoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsoStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
//StreamResourceInfo sri = Application.GetResourceStream(fileStream);
bitmap = PictureDecoder.DecodeJpeg(fileStream);
}
}
return bitmap;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.