【问题标题】:Show image from URL with Xamarin.Forms使用 Xamarin.Forms 从 URL 显示图像
【发布时间】:2016-12-19 00:57:34
【问题描述】:

我正在使用此代码显示来自 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>

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

【问题讨论】:

  • 我刚刚测试了它,它可以工作。

标签: c# image xamarin url xamarin.forms


【解决方案1】:

你可以用你的 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"/>

并去掉后面代码中相关的代码。兑现默认开启24小时

【讨论】:

  • 在 IOS 设备上工作正常,但在 android 设备上不工作
  • 如果你把它放在一个stacklayout而不是absolute中怎么样?并在不同的设备上试用
【解决方案2】:

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

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

并更新所有 Xamarin.Android 包

【讨论】:

  • 对我不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2019-05-25
  • 2019-02-17
  • 1970-01-01
相关资源
最近更新 更多