首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Xamarin Forms CollectionView图像源未绑定到文件路径。(Xamarin Forms预发行版本v4.0.0.8055-pre1)

Xamarin Forms CollectionView图像源未绑定到文件路径。(Xamarin Forms预发行版本v4.0.0.8055-pre1)
EN

Stack Overflow用户
提问于 2019-01-07 21:38:42
回答 1查看 582关注 0票数 0

如果我在XAML中有一个简单的图像:

代码语言:javascript
运行
复制
<Image
    Source="{Binding ImageUrl}"
    Grid.Column="0"
    HorizontalOptions="EndAndExpand"
    VerticalOptions="CenterAndExpand" />

文件路径正确绑定。但是,如果我将图像元素嵌套在CollectionView中,图像将不再绑定。

代码语言:javascript
运行
复制
<CollectionView
    x:Name="CV"
    ItemsSource="{Binding ThumbnailPhotos}"
    VerticalOptions="Center"
    HorizontalOptions="Center">
    <CollectionView.ItemsLayout>
        <GridItemsLayout
            Orientation="Vertical"
            Span="2" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Frame
                BorderColor="LightGray"
                CornerRadius="3"
                HasShadow="False">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition
                            Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition
                            Height="Auto" />
                    </Grid.RowDefinitions>
                    <Image
                        Source="{Binding ImageUrl}"
                        Grid.Column="0"
                        HorizontalOptions="EndAndExpand"
                        VerticalOptions="CenterAndExpand" />
                </Grid>
            </Frame>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

出于测试目的,我的ViewModel中的ObservableCollection如下所示:

代码语言:javascript
运行
复制
ThumbnailPhotos = new ObservableCollection<Thumbnail>
{
    new Thumbnail(){ ImageUrl = "/Users/<myUser>/Library/Developer/CoreSimulator/Devices/68BF4E8E-DF6D-4459-B6AD-DCEDB270916B/data/Containers/Data/Application/0C205001-6DFD-4846-9E6D-415F772FCB58/Documents/Defect Photos/IMG_20190102_165617/IMG_20190102_165617.jpg" } ,
    new Thumbnail(){ ImageUrl = "/Users/<myUser>/Library/Developer/CoreSimulator/Devices/68BF4E8E-DF6D-4459-B6AD-DCEDB270916B/data/Containers/Data/Application/0C205001-6DFD-4846-9E6D-415F772FCB58/Documents/Defect Photos/IMG_20190102_165617/IMG_20190102_165617.jpg" } ,
    new Thumbnail(){ ImageUrl = "/Users/<myUser>/Library/Developer/CoreSimulator/Devices/68BF4E8E-DF6D-4459-B6AD-DCEDB270916B/data/Containers/Data/Application/0C205001-6DFD-4846-9E6D-415F772FCB58/Documents/Defect Photos/IMG_20190102_165617/IMG_20190102_165617.jpg" } ,
    new Thumbnail(){ ImageUrl = "/Users/<myUser>/Library/Developer/CoreSimulator/Devices/68BF4E8E-DF6D-4459-B6AD-DCEDB270916B/data/Containers/Data/Application/0C205001-6DFD-4846-9E6D-415F772FCB58/Documents/Defect Photos/IMG_20190102_165617/IMG_20190102_165617.jpg" } ,
};
EN

Stack Overflow用户

发布于 2019-01-08 19:03:10

错误似乎在于构建文件路径的方式。在iOS上,我最终使用了这种方法:

代码语言:javascript
运行
复制
var docsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var photosPath = Path.Combine(docsPath, "DefectPhotos/");

        if(Directory.Exists(photosPath))
        {
            string[] photoNames = GetFileNames(photosPath, "*.jpg");

            foreach (var photoName in photoNames)
            {
                if (photoName.Contains(_item.DefectId))
                {
                    thumbnailPhotos.Add(new Thumbnail() { ImageUrl = photosPath + photoName });
                }
            }
        }

关键部分是获取文档路径的方式。我在试图加载的photoName上使用"get info“来测试图像路径。该文件路径不起作用。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54075493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档