简体   繁体   English

Xamarin Forms iOS-显示资产目录中的图像

[英]Xamarin Forms iOS - Display image from asset catalog

I know that there are serveral questions on SO covering the topic, however, so far they didn't help me to solve the problem. 我知道在SO上存在涉及该主题的服务器问题,但是到目前为止,它们并没有帮助我解决问题。 I want to display an image in xamarin forms. 我想以xamarin形式显示图像。 On android it works, on iOS the image wont show up. 在android上可以使用,在iOS上不会显示图像。 I use asset catalog to define the image in iOS. 我使用资产目录在iOS中定义图像。 Please see below: 请看下面:

Xaml: Xaml:

<Image Opacity="0.4" Source="happinessfactury.png" Aspect="AspectFill" />

iOS project file: iOS项目文件:

<ImageAsset Include="Assets.xcassets\happinessfactury.imageset\Contents.json" />
<ImageAsset Include="Assets.xcassets\happinessfactury.imageset\happinessfactory@1x.png" />
<ImageAsset Include="Assets.xcassets\happinessfactury.imageset\happinessfactory@2x.png" />
<ImageAsset Include="Assets.xcassets\happinessfactury.imageset\happinessfactory@3x.png" />

contents.json contents.json

{"images": [
{
  "idiom": "universal"
},
{
  "filename": "happinessfactory@1x.png",
  "scale": "1x",
  "idiom": "universal"
},
{
  "filename": "happinessfactory@2x.png",
  "scale": "2x",
  "idiom": "universal"
},
{
  "filename": "happinessfactory@3x.png",
  "scale": "3x",
  "idiom": "universal"
},
{
  "idiom": "iphone"
},
{
  "scale": "1x",
  "idiom": "iphone"
},
{
  "scale": "2x",
  "idiom": "iphone"
},
{
  "subtype": "retina4",
  "scale": "2x",
  "idiom": "iphone"
},
{
  "scale": "3x",
  "idiom": "iphone"
},
{
  "idiom": "ipad"
},
{
  "scale": "1x",
  "idiom": "ipad"
},
{
  "scale": "2x",
  "idiom": "ipad"
},
{
  "idiom": "watch"
},
{
  "scale": "2x",
  "idiom": "watch"
},
{
  "screenWidth": "{130,145}",
  "scale": "2x",
  "idiom": "watch"
},
{
  "screenWidth": "{146,165}",
  "scale": "2x",
  "idiom": "watch"
},
{
  "idiom": "mac"
},
{
  "scale": "1x",
  "idiom": "mac"
},
{
  "scale": "2x",
  "idiom": "mac"
}],"info": {
"version": 1,
"author": "xcode"},"properties": {
"on-demand-resource-tags": [
  "happinessfactury"
]}}

EDIT 1 In Xaml I mistakenly set the source to Source="happinessfactury" which was wrong. 编辑1在Xaml中,我错误地将源设置为Source =“ happinessfactury”,这是错误的。 However even with Source="happinessfactury.png" it is not working on iOS. 但是,即使使用Source =“ happinessfactury.png”,它也无法在iOS上运行。 (On android though) (虽然在android上)

EDIT 2 It's a little bit embarassing, but I solved the issue. 编辑2这有点尴尬,但我解决了这个问题。 The mistake was to reference the image in xaml by happinessfactury instead of happinessfactory which is the correct name and also the name of the image files. 这个错误是由引用在XAML图像happinessfactury代替happinessfactory这是正确的名称,并在图像文件的名称。

First off, I'm a little confused as to if you are using Xamarin.Forms or Xamarin.iOS. 首先,对于您使用的是Xamarin.Forms还是Xamarin.iOS,我有些困惑。 But one possible solution when using Xamarin.forms with PCL, is to simply add the images you wish to display to an Assets folder in your Portable project. 但是,将Xamarin.forms与PCL一起使用时,一种可能的解决方案是将您希望显示的图像简单地添加到Portable项目中的Assets文件夹中。 And then set the image source in code behind using a ImageSource.FromResource() like so: 然后像下面这样使用ImageSource.FromResource()在代码中设置图像源:

MyImg.Source = ImageSource.FromResource("ProjectName.Assets.happinessfactury.png");

Another source that might help is the Book Creating Mobile Apps with Xamarin.Forms 另一个可能有用的资源是《 使用Xamarin.Forms创建移动应用程序 》一书

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM