使用Xamarin.Forms从URL显示图像 [英] Show image from URL with Xamarin.Forms

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

问题描述

我正在使用此代码显示来自URL的图像

I am using this code to show image from an URL

<?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="LandAHand.VolunteerView">
    <ContentPage.Content>
         <AbsoluteLayout BackgroundColor="Maroon">
             <Image x:Name="backgroundImage" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" />
         </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

.cs

using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace LandAHand
{
    public partial class VolunteerView : ContentPage
    {
        public VolunteerView()
        {
            InitializeComponent();
            backgroundImage.Source = new UriImageSource
            {
                Uri = new Uri("https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg"),
                CachingEnabled = true,
                CacheValidity = new TimeSpan(5, 0, 0, 0)
            };
        }
    }
}

此代码已成功在iOS上运行,但不适用于Android.

This code is successfully working with iOS but it is not working with Android.

推荐答案

使用Xaml,您可以更轻松地完成此操作,只需将您的xaml像这样

Well you can do this thing easier with your Xaml just make your xaml like this

<Image x:Name="backgroundImage" Source="https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg" AbsoluteLayout.LayoutBounds="0,0,1,1"   AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill"/>

并删除后面代码中的相关代码.默认情况下,自动启用兑现24小时

And remove the code related in the code behind. The cashing is enabled by default for 24 hours

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

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