使用Xamarin Forms显示图像 [英] Displaying an image with Xamarin Forms

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

问题描述

已解决: 答案是更新所有nuget程序包,并以较新版本的Android为目标.现在,图像将按预期加载.我对此不满意,因为我完全使用了Xamarin提供的代码,并且以较新版本为目标已经弃用了代码所依赖的某些项目.最初的版本是Xamarin.Forms v23,我更新到了V25

我有一个全新的Xamarin表单项目,具有一个简单的视图,试图在其中显示图像.我尝试了几种方法来显示图像,但我一点也没有运气.

I have a brand new Xamarin forms project with a simple view in which I'm trying to display an image. I've tried several ways of getting an image to display and I am not having any luck at all.

我正在使用<image>,我也尝试过FFImageLoader控件.

I'm using <image> and I have also tried FFImageLoader control as well.

<StackLayout Orientation="Vertical">

        <ff:CachedImage Source="https://static.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg" WidthRequest="100" HeightRequest="100" />

        <Button x:Name="btn" Text="Image" Clicked="Button_Clicked" />

        <Frame OutlineColor="Red">
            <Image x:Name="StupidImage" Source="{Binding Thumbnail}"  Aspect="Fill" HeightRequest="100" WidthRequest="100"   />
        </Frame>

        </StackLayout>

这是当前视图.我还直接将Source设置为没有结果的值.

This is the current view. I've also set the Source directly to a value with no result.

我能够获得图像的流.我能够从流中读取所有字节.我构建了一个调试可视化器,以将字节显示为图像.从来源获取图像不是问题.获取图像控件以显示图像是一个问题.

I'm able to get a stream for the image. I'm able to read all of the bytes from the stream. I built a debug visualizer to display the bytes as an image. Getting the image from a source is not a problem. Getting the image control(s) to display the image is a problem.

我尝试与视图模型绑定.当失败时,我尝试直接设置来源

I tried binding with a view model. When that failed, I tried that directly setting the source

StupidImage.Source = ImageSource.FromStream(() => result.Stream);

我还制作了字节的副本并尝试了

I also made a copy of the bytes and tried

StupidImage.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));

我尝试了ImageSource.FromFile().FromUri.我尝试将图像作为资源添加到项目中.每次尝试都相同,读取了资源并且字节可用,但是图像控件只是不显示它.

I've tried ImageSource.FromFile() and .FromUri. I tried adding an image to the project as a resource. Each try was the same, the resource was read and the bytes were available, but the image control just doesn't display it.

我认为这可能是大小问题,因此我设置了控件的大小.没有.我以为这可能是分辨率问题,所以我使用了较小的图像.我尝试了几种不同质量的图像.

I thought maybe it was a size problem, so I set the size of the control. Nothing. I thought maybe it was a resolution problem, so I used a smaller image. I tried several different images of varying quality.

然后我放弃了图像控件,得到了FFImageLoading nuget包,并给了它直接指向图像的URL.与FFImageLoading示例使用的示例相同.仍然没有图像.

Then I gave up on the image control and I got the FFImageLoading nuget package and gave it a direct url to an image. Same example that FFImageLoading examples used. Still no image.

我尝试了模拟器,并尝试了2种不同的物理设备.结果相同.

I tried the emulator and I tried 2 different physical devices. Same result.

我还尝试使用btn.Image = "whatever.jpg"在按钮上设置图像,结果相同.

I also tried setting an image on a button using btn.Image = "whatever.jpg" with the same result.

这是每次的结果.我迷路了.如何显示图像?

This is the result every time. I'm lost. How do I get images to display?

我确实做到了这一点,但仅限于模拟器

I did get this to work, but only on the emulator

<Image x:Name="StupidImage" Source="https://static.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg" />

StupidImage.Source = ImageSource.FromUri(new Uri("https://static.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg"));

编辑2-澄清

我的目标是允许用户从设备中选择照片,然后显示其预览.

My goal is to allow the user to select a photo from the device and then display a preview of it.

推荐答案

如果您想在应用中使用图片,则可以将其加载到共享项目中,例如

If you want to use images in you app you can load them into your Shared Project, like

请确保将构建操作更改为嵌入式资源

然后在您的代码中

image.Source = ImageSource.FromResource("App5.Images.useravatar.png");

记下资源名称.

和XAML

<ContentPage.Content>
    <StackLayout>
        <Image x:Name="image" WidthRequest="50"/>
    </StackLayout>
</ContentPage.Content>

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

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