我已经尝试了各种方法来绑定gird项模板中的动态图像的路径,似乎没有任何工作。照片路径在图片文件夹中的事实是否重要?下面是我目前的代码,任何指针或反馈都是非常感谢:
I have tried all kinds of ways of binding the path of dynamic images in the gird item template and nothing seems to work. Does the fact that the photo path are in the Pictures folder matter? Below is the code I currently have, any pointer or feedback are greatly appreciated:
XML
< RelativePanel> < GridView x:Name =" AlbumPhotos" SelectionMode =" Multiple" < GridView.ItemTemplate> < DataTemplate x:Name =" ImageGalleryDataTemplate" x:DataType =" storage:StorageFile"> < Image Source =" {x:Bind Path}"高度= QUOT; 180"宽度= QUOT; 180" Stretch =" UniformToFill" /> < / DataTemplate> < /GridView.ItemTemplate> < GridView.ItemsPanel> < ItemsPanelTemplate> < ItemsWrapGrid MaximumRowsOrColumns =" 6" Orientation =" Horizontal" /> < / ItemsPanelTemplate> < /GridView.ItemsPanel> < / GridView> < / RelativePanel>
<RelativePanel> <GridView x:Name="AlbumPhotos" SelectionMode="Multiple"> <GridView.ItemTemplate> <DataTemplate x:Name="ImageGalleryDataTemplate" x:DataType="storage:StorageFile"> <Image Source="{x:Bind Path}" Height="180" Width="180" Stretch="UniformToFill"/> </DataTemplate> </GridView.ItemTemplate> <GridView.ItemsPanel> <ItemsPanelTemplate> <ItemsWrapGrid MaximumRowsOrColumns="6" Orientation="Horizontal"/> </ItemsPanelTemplate> </GridView.ItemsPanel> </GridView> </RelativePanel>
C#
推荐答案
您好,
您无法将路径绑定到 Image.Source 属性直接。您需要将其转换为BitmapImage对象并将BitmapImage绑定到Image.Source属性。像这样:
You could not bind the path to the Image.Source property directly. You need to convert it to a BitmapImage object and bind the BitmapImage to the Image.Source property. Like this:
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { // Set the image source to the selected bitmap BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); }然后像这样绑定bitmapimage:
Then bind the bitmapimage like this:
<Image Source="{x:Bind bitmapImage}" Height="180" Width="180" Stretch="UniformToFill"/>您可以在此处获取有关如何加载图像文件的更多信息:图像类
You could get more information about how to load an image file here:Image Class
最好的问候,
Roy
更多推荐
[UWP] [Xaml] Gridview显示图像库不工作
发布评论