问题描述
我正在尝试使用按钮中的image属性添加背景图像.我面临的问题是我无法将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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!