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

查看:26
本文介绍了将图像绑定到 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>

查看模型:

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 到.

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

Change public UriImageSource ProductImage { get;放;} to public Uri ProductImage { get;放;}

Change public UriImageSource ProductImage { get; set; } to public Uri ProductImage { get; set; }

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

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