显示来自URL Xamarin.Forms的图像

Kis*_*har 8 c# android xamarin.android xamarin xamarin.forms

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

的.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="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>
Run Code Online (Sandbox Code Playgroud)

的.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)
            };
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码成功使用iOS但不使用android.

Ahm*_*adi 7

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

<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"/>
Run Code Online (Sandbox Code Playgroud)

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

  • 在IOS设备上正常工作,但在android设备上不工作 (2认同)

小智 6

它不起作用,因为您使用 https 网址。要修复它,您应该像这样配置您的 Android 项目:在项目选项 > Android 选项中,单击高级选项 HttpClient 实现:选择 Android SSL/TLS 实现:选择本机 TLS 1.2+

https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=windows

并更新所有 Xamarin.Android 包