如何将 ImageSource 设置为 Xamarin.Forms.Button? [英] How to set ImageSource as Xamarin.Forms.Button?

查看:22
本文介绍了如何将 ImageSource 设置为 Xamarin.Forms.Button?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用按钮中的图像属性添加背景图像.我面临的问题是我无法将 StreamImageSource 设置为按钮背景.如果我尝试这样做,我会遇到下面给出的错误.

I am trying add a background image using the image property in button. The issue I'm facing is that i can't set StreamImageSource as button background. I encountered the error given below if I try to do so.

我用来设置图像的代码:

            ImageSource iconsource =ImageSource.FromStream(() => new MemoryStream(ImgASBytes));
            Button Icon = new Button ();
            Icon.Image = iconsource ;

我遇到的错误:

错误 CS0266:无法将类型Xamarin.Forms.ImageSource"隐式转换为Xamarin.Forms.FileImageSource".存在显式转换(您是否缺少演员表?)

Error CS0266: Cannot implicitly convert type 'Xamarin.Forms.ImageSource' to 'Xamarin.Forms.FileImageSource'. An explicit conversion exists (are you missing a cast?)

推荐答案

ImageSource.FromStream() 返回一个 StreamImageSource(参见 文档).Button.Image 仅接受 FileImageSource(请参阅 文档).

ImageSource.FromStream () returns a StreamImageSource (see docs). Button.Image accepts only FileImageSource (see docs).

这意味着,无论您多么努力地将其中的一个扔到另一个中,您想要实现的目标都不会奏效.

It means that what you're trying to achieve won't work, no matter how hard you try to cast one into the other.

Button.Image 将接受作为资源存储在您的平台项目中的图像,并加载:

Button.Image will accept images stored as resources in your platform projects, and loaded either with:

Icon.Image = ImageSource.FromFile ("foobar.png");

Icon.Image = "foobar.png";

这篇关于如何将 ImageSource 设置为 Xamarin.Forms.Button?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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