【问题标题】:Bind Image to URL Xamarin Forms XAML将图像绑定到 URL Xamarin 表单 XAML
【发布时间】:2023-04-04 20:53:01
【问题描述】:

我在使用 Xamarin 表单将图像绑定到 UriImageSource 时遇到问题。

我已经实现了 FlowListView,它显示了一个类似网格的文本列表,但与每个产品关联的图像没有出现。

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

XAML:

<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 初始化时

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")
    }
});

(在上面的例子中,“测试”会出现,但图像不会)

【问题讨论】:

  • @jzeferino 知道了,谢谢!!

标签: image xaml xamarin data-binding xamarin.forms


【解决方案1】:

由于您将 UriImageSource 绑定到 Uri 您的 ViewModel 属性必须是一个 Uri。

将public UriImageSource ProductImage { get; set; } 更改为public Uri ProductImage { get; set; }

【讨论】:

    【解决方案2】:

    遇到了同样的问题,不知道有没有改变,但是我发现你可以将strait绑定到图像源,如下所示:

    <Image HeightRequest="200" Source="{Binding ProductImage}"/>
    

    然后在后面的代码中,您可以使用 var,并让 C# 进行正确的转换:

       public ImageSource ProductImage {
            get {
                var source =  new Uri("http://myurl.com/one.jpg");
                return source;
            }
        }
    

    我喜欢这个,如果你想让你的代码使用资源中的图像,我们只需像这样更改源代码行:

        public ImageSource ProductImage {
            get {
                var source = "resource_image.png"
                return source;
            }
        }
    

    请记住,在 MacOS 上的 Xamarin 中通过 TLS(https url)加载图像似乎存在问题(截至 2018 年),因此我建议坚持使用 http 或在 windows 上构建。

    【讨论】:

    • 您能否详细说明您的最后一段,因为我似乎遇到了这个问题,如下所述:stackoverflow.com/questions/58312494/… +1。
    • 当我在芝加哥以外的一个合同项目上工作时,我们意识到了这一点。团队中的大多数其他成员都傻眼了,相同的代码在他们的 Windows 机器上运行,但在 Mac 上可靠地失败了。原来是某种错误。 2018 年就是这种情况,因此较新版本的 Xamarin 可能已更正了该错误。
    • @w0051977 我不相信这是同一个问题,因为您没有使用 https/TLS。就我个人而言,我主要在 Windows 上坚持使用 Xamarin,或者使用其他框架。也许在 mac 上的 vm 中运行 windows?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2017-01-15
    • 2020-11-20
    • 2017-11-18
    • 2019-08-17
    • 2017-01-18
    相关资源
    最近更新 更多