简体   繁体   English

Xamarin.Forms:使用资产目录中的图像

[英]Xamarin.Forms: Use image from asset catalog

I have a Master Detail page and I want to set the icon in the navigation bar to show the menu. 我有一个主详细信息页面 ,我想在导航栏中设置图标以显示菜单。 Therefore I put my images into an asset catalog (Asset Catalogs > Media > menu). 因此,我将我的图像放入资产目录(资产目录>媒体>菜单)。 If I want to reference that image from XAML it doesn't show up, when the image is in the asset catalog. 如果我想从XAML引用该图像,则当图像位于资产目录中时,它不会显示。

I tried different paths, but they all don't work. 我尝试了不同的路径,但它们都不起作用。 Debugging also doesn't show any images. 调试也不显示任何图像。 It seems that Xamarin.Forms doesn't support asset catalogs, but that is not true. 似乎Xamarin.Forms不支持资产目录,但事实并非如此。 On a normal content page I have this: 在正常的内容页面上,我有这个:

<ContentPage.ToolbarItems>
 <ToolbarItem Text="Configuration" Activated="OpenConfiguration" Order="Primary" Priority="0">
  <ToolbarItem.Icon>
    <OnPlatform x:TypeArguments="FileImageSource">
      <OnPlatform.iOS>gear.png</OnPlatform.iOS>
      <OnPlatform.Android>gear.xml</OnPlatform.Android>
      <OnPlatform.WinPhone>Assets/gear.png</OnPlatform.WinPhone>
    </OnPlatform>
  </ToolbarItem.Icon>
 </ToolbarItem>
</ContentPage.ToolbarItems>

This is working without problems. 这没有问题。 On my MasterPage (page, which is responsible for showing the menu entries in the master detail template) I have this: 在我的MasterPage (页面,负责显示主详细信息模板中的菜单条目)我有:

<ContentPage.Icon>
 <OnPlatform x:TypeArguments="FileImageSource">
  <OnPlatform.iOS>menu.png</OnPlatform.iOS>
  <OnPlatform.Android>menu.xml</OnPlatform.Android>
  <OnPlatform.WinPhone>Assets/menu.png</OnPlatform.WinPhone>
 </OnPlatform>
</ContentPage.Icon>

Bascially, it is the same code, but the icon doesn't show up. 基本上,它是相同的代码,但图标不会显示。 I get the icon only displaying if I put the images into Resources folder ... 如果我将图像放入Resources文件夹,我只显示图标...

But how can I use the images from the asset catalog in my XAML? 但是如何在XAML中使用资产目录中的图像?

The only response I got from Xamarin is, that FileImageSource can't be used with an asset catalog. 我从Xamarin得到的唯一回应是, FileImageSource不能与资产目录一起使用。 That isn't completely true, but since it doesn't work every time one shouldn't use it. 这不完全正确,但因为每次不使用它都不起作用。

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

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