11

I am having trouble with xamarin folders. Currently I'm writing xamarin iOS project. In Xcode I used directories for grouping images, there could be several levels of nested folders, but when I was building project for device or iOS simulator, these resources where simply being copied to main bundle, without any folder structure. I can't reach the same behaviour in xamarin studio. Whenever I create folders in my project and put pictures or other resources in them, this folder structure is recreated on the actual device, and thus, I struggle against different paths, when loading images. How can I make xamarin studio simply copy the files in the folders to main bundle, instead of recreating folder structure?

Thanks for help.

kyurkchyan
  • 2,260
  • 2
  • 23
  • 37
  • FWIW, Xcode's "Folders" aren't really folders, they are just file groupings and do not reflect on-disk file structure. Xamarin Studio does not support this concept of virtual file groupings and so using subfolders does not work quite the way you expect. However, using real folders in Xcode works the same as folders in Xamarin Studio. – jstedfast Jun 19 '13 at 17:49

2 Answers2

16

My first suggestion is to change the BuildAction property of your images to BundleResource.

Once you do that, there are multiple ways of achieving your goal:

The first option is to specify a LogicalName to be whatever you want the name to be inside of the compiled app bundle. Currently there's no way to set the Resource ID (UI name for the LogicalName property) for anything other than EmbeddedResource files (I'll work on fixing that momentarily), but you can edit the *.csproj like so:

<BundleResource Include="Icons\icon.png">
  <LogicalName>icon.png</LogicalName>
</BundleResource>

Normally, that Icons\icon.png file would be copied into the iOS app bundle as Icons/icon.png, however, the LogicalName property overrides the relative path name. In this case it would be copied over as simply icon.png.

As another example, you can also do this:

<BundleResource Include="Icons\iOS\icon.png">
  <LogicalName>AppIcon.png</LogicalName>
</BundleResource>

This will copy the Icons\iOS\icon.png file into the root of the iOS app bundle and also rename it to AppIcon.png.

A second option is to simply move your image file(s) into the Resources folder. The Resources folder is special directory that get stripped out of the default path names when copied over to the iOS app bundle. In other words, Resources\icon.png would be copied over into the root of the iOS app bundle as icon.png rather than Resources\icon.png as is the case with normal project directories.

A third option is to simply register other "Resource" directories of your own (and they can exist within other directories, including the default Resources directory).

For example, you could have the structure in your project:

Resources/
   Icons/
      icon.png
      icon@2x.png

And in your *.csproj file, edit the following tag:

<IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>

and replace it with:

<IPhoneResourcePrefix>Resources;Resources\Icons</IPhoneResourcePrefix>

This will ensure that the icon.png and icon@2x.png files are installed in the root of the iOS app bundle.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Sorry after you've edited the answer on Jun 19, I didn't notice your answer. almost 6 months had already passed since then :)) I've just seen this. really great answer! thanks! – kyurkchyan Dec 24 '13 at 06:23
  • Where is all of this documented BTW? I really miss the run script build phase from Xcode, and I'm looking for sane ways of doing various stuff during builds. – Stefan Fisk Jan 25 '14 at 13:33
  • This is a great answer - and it's helped me cleanup my resources folder. Thanks! – Ender2050 Sep 17 '14 at 21:41
  • You would need to have both combinations in your `< IPhoneResourcePrefix>` if you're working on both Xamarin Studio on Mac and VS on Windows: `Resources;Resources/Icons;Resources\Icons` – manman Jan 06 '15 at 17:39
  • You actually only need "Resources\Icons" (updated my answer) since Xamarin Studio will convert that into a Unix path on Mac. – jstedfast Jan 06 '15 at 18:25
1

Xamarin has two ways to setup files you want present in the iOS bundle:

  1. Put them in any folder, and mark the "Build Action" as "Content". Whatever directory structure you have in your project will be present in the main bundle.
  2. Put them in the "Resources" folder, with a "Build Action" as "BundleResource", this does the same as #1, but removes the "Resources" folder from the path present in the bundle. This is a nice place to put all images you want in the root of your bundle but would clutter up your project.
jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • Yes but I if I put some folders inside resources folder, they are also copied to bundle. So only way is to just collect all images in one folder??? What a stupid thing. – kyurkchyan Jun 05 '13 at 12:02
  • I'm not following what you want. Are you wanting to have a folder structure in the project, but a flat structure in the bundle? – jonathanpeppers Jun 05 '13 at 17:32
  • Yeah exactly! Like it is in Xcode. I use resource groups or folders their, but in any case the contents are copied in flat structure in bundle. – kyurkchyan Jun 05 '13 at 18:08
  • Use solution folders in Xamarin Studio. These are virtual folders that do not exist in the file system. – Krumelur Jun 05 '13 at 19:50
  • And how can I add solution folder? Only Thing available in context menu when right clicking project is New Folder, which adds and ordinary folder. – kyurkchyan Jun 06 '13 at 04:14
  • Solution folders only group projects, so I don't think @Krumelur's option will help you. There is a "logical" name or something on items set as BundleResource, I'll take a look today and see if you can remove subfolders. – jonathanpeppers Jun 06 '13 at 12:10
  • Using the LogicalName property on BundleResource items should work for what he needs. – jstedfast Jun 07 '13 at 17:28