将图像绑定到URL Xamarin Forms XAML [英] Bind Image to URL Xamarin Forms XAML

查看:97
本文介绍了将图像绑定到URL Xamarin Forms XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Xamarin Forms将图像绑定到UriImageSource。

I am having trouble binding an Image to a UriImageSource using Xamarin Forms.

我已经实现了FlowListView,它提供了一个类似网格的文本列表,但相关的图像每个产品都没有出现。

I have implemented the FlowListView which presents a grid-like list of text but the images associated with each product aren't appearing.

还有其他人有这个/知道如何解决它吗?

Has anyone else had this/know how to work around it?

<flv:FlowListView 
    Grid.Row="1" 
    x:Name="flvListView" 
    SeparatorVisibility="None" 
    HasUnevenRows="true" 
    FlowColumnMinWidth="110">
    <flv:FlowListView.FlowColumnTemplate>
        <DataTemplate>
            <Grid Padding="3">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Image HeightRequest="100" >
                    <Image.Source>
                        <UriImageSource Uri="{Binding ProductImage}" />
                    </Image.Source>
                </Image>
                <Label 
                    x:Name="Label" 
                    HorizontalOptions="Fill" 
                    HorizontalTextAlignment="Center" 
                    VerticalOptions="End" 
                    BackgroundColor="Silver"
                    Opacity="0.5" Text="{Binding ProductName}"/>
            </Grid>
        </DataTemplate>
    </flv:FlowListView.FlowColumnTemplate>
</flv:FlowListView>



查看型号:



View Model:

public class vmProduct
{
    public vmProduct() { }
    /* removed other properties, constructors etc */
    public UriImageSource ProductImage { get; set; }
    public string ProductName { get; set; }
}

初始化vmProduct时

When the vmProduct is initialised

this.Products.Add(new vmProduct
{
    ProductName = "Test",
    ProductImage = new UriImageSource
    {
        Uri = new Uri("https://upload.wikimedia.org/wikipedia/en/5/5f/Original_Doge_meme.jpg")
    }
});

(在上面的示例中,将出现Test但图像不会出现)

(In the above example, "Test" will appear but the image will not)

推荐答案

因为您将 UriImageSource 绑定到 Uri 您的 ViewModel 属性必须是Uri to。

Since you are binding the UriImageSource to a Uri your ViewModel property must be a Uri to.

更改 public UriImageSource ProductImage {get;组; } public Uri ProductImage {get;组; }

这篇关于将图像绑定到URL Xamarin Forms XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆