简体   繁体   English

从URI xamarin表单的列表视图中加载图像

[英]Load an image in a listview from URI xamarin forms

ok, so I have this listView which is supposed to show a bunch of items, each of which containing at least one photo. 好的,所以我有这个listView,它应该显示一堆项目,每个项目至少包含一张照片。

The idea is to show the main photo in the listCell, and when an item is selected, its details are shown in a different Forms Page, and there its supposed to be able to access all its photos. 这个想法是在listCell中显示主照片,并且当选择一个项目时,其详细信息将显示在不同的“表单页面”中,并且应该可以访问其所有照片。

When the item doesn't have a photo, it will show a placeholder one from resources. 当该项目没有照片时,它将从资源中显示一个占位符。

Problem: can't load the image from URI either binding the image source to a list property (from the viewModel) that contains the specific URI obj, or by binding it to the same property containing now strings, or by means of 问题:无法将图像源从URI加载,要么将图像源绑定到包含特定URI obj的列表属性(来自viewModel),要么无法将其绑定到包含现在字符串的相同属性,或者无法通过

<Image.Source>
     <UriImageSource Uri="{Binding MainPhotoSource}" />
</Image.Source>

no matter. 不管。 none of these seems to work. 这些似乎都不起作用。

already asked the Xamarin Team for help, and their answer was to come here, or go to the forums (which I already did, been waiting for almost two months, now, and the work needs to be delivered)... 已经向Xamarin团队寻求帮助,他们的答案是到这里来,或者去论坛(我已经做了,已经等待了将近两个月,现在需要交付工作)...

any help, please? 有什么帮助吗?

EDIT: Here's a piece of the ViewModel code. 编辑:这是ViewModel代码的一部分。

In this first method, for each item I receive from the WCF, I add an equivalence in the format of this ItemDto obj to this ObservableCollection List. 在第一种方法中,对于我从WCF收到的每个项目,我都以此ItemDto obj的格式将等效项添加到此ObservableCollection List。

// Sets this List observable collection to a new ItemDto obj,
// with certain properties from the Item.Find result[].
ObservableCollection<ItemDto> SetList(Item.Find[] result)
{
    ObservableCollection<ItemDto> collection = new ObservableCollection<ItemDto>();

    foreach (Item.Find item in result)
    {
        collection.Add(GetDto(item));
    }
    return collection;
}

// Gets a new ItemDto obj with the required fields from Item.Find obj.
ItemDto GetDto(Item.Find item)
{
    return new ItemDto()
    {
        ID = item.ID,
        UserID = item.User_ID,
        MainPhotoSource = new Uri(_serverInbox + item.MediaItems[0].RelativeUrl),
        Title = item.Title,
        Description = item.Description,
        Category = item.Category_Name,
        DateCreated = GetFormatedDateCreated(item.DateCreated)
    };
}

Uri property of UriImageSource requires an Uri rather than a string. UriImageSource的Uri属性需要Uri而不是字符串。 But you can use a URI Bindable property in your View Model and bind to it: 但是您可以在视图模型中使用URI Bindable属性并将其绑定到:

Check this code 检查此代码

View Model 查看模型

public string ProductNo
{ 
    get { return _productNo}
    set
    {
        if (_productNo != value)
        {
            _productNo = value;
            RaisePropertyChanged();
            RaisePropertyChanged(() => ThumbnailImageUri);
        }
    }
}

public Uri ThumbnailImageUri
{
    get
    {
        if (_thumbnailImageUri == null)
        {
            _thumbnailImageUri = new Uri(String.Format("http://www.YOURWEBSITE.com/{0}.jpg", _productNo));
        }
        return _thumbnailImageUri;
    }            
}

View 视图

<StackLayout BindingContext="{Binding SelectedProduct}">
    <StackLayout Orientation="Horizontal">
      <Image HorizontalOptions="EndAndExpand"
           VerticalOptions="Center">
        <Image.Source>
          <UriImageSource Uri="{Binding ThumbnailImageUri}"/>
        </Image.Source>
      </Image>    
    <Label Text="{Binding ProductNo}"
         Font="Bold, Large"
         HorizontalOptions="StartAndExpand"
    VerticalOptions="Center"/>
    </StackLayout>
</StackLayout>

Here is, what works for me - hope this helps you 这是对我有用的-希望这对您有帮助

First my BindingContext: 首先我的BindingContext:

public class ItemContainer
{
    public ItemContainer()
    {           
        Collection = SetList(new[] { "1", "2", "3", "4" });
    }

    ObservableCollection<ItemDto> SetList(string[] result)
    {
        ObservableCollection<ItemDto> collection = new ObservableCollection<ItemDto>();

        foreach (string item in result)
        {
            collection.Add(GetDto(item));
        }
        return collection;
    }
    public ObservableCollection<ItemDto> Collection { get; set; }
    ItemDto GetDto(string item)
    {
        return new ItemDto() { MainPhotoSource = new Uri(_serverInbox + item) }; 
    }
}

My Page1.xaml looks like this: 我的Page1.xaml看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1gcm.Page1">
    <ListView ItemsSource="{Binding Collection}" VerticalOptions="Center" HorizontalOptions="Center" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Image Source="{Binding MainPhotoSource}" />
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

And I merge them on creating as MainPage in App.cs: 我将它们合并为App.cs中的MainPage:

public App()
{
    // The root page of your application
    MainPage = new Page1
    {
        BindingContext = new ItemContainer()
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM