Xamarin表单iOS - 显示资产目录

时间:2018-04-06 20:33:22

标签: xamarin xamarin.forms xamarin.ios xcasset

我知道关于这个主题的SO有几个问题,但到目前为止,它们并没有帮我解决问题。我想以xamarin形式显示图像。在Android上它可以工作,在iOS上图像不会出现。我使用资产目录在iOS中定义图像。请参阅以下内容:

的Xaml:

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

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

{"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"
]}}

编辑1 在Xaml中,我错误地将源设置为Source =“happinessfactury”,这是错误的。然而,即使使用Source =“happinessfactury.png”,它也无法在iOS上运行。 (虽然在Android上)

编辑2 这有点令人尴尬,但我解决了这个问题。错误是通过happinessfactury而不是happinessfactory引用xaml中的图像,这是正确的名称以及图像文件的名称。

1 个答案:

答案 0 :(得分:0)

首先,如果您使用的是Xamarin.Forms或Xamarin.iOS,我有点困惑。但是,将Xamarin.forms与PCL一起使用时,一种可能的解决方案是将您想要显示的图像添加到Portable项目的Assets文件夹中。然后使用像这样的ImageSource.FromResource()在后面的代码中设置图像源:

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

可能有用的另一个来源是图书Creating Mobile Apps with Xamarin.Forms