Search code examples
iosxamarin.iosxamarinxamarin-studio

Xamarin studio folder structure issue in iOS project


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.


Solution

  • 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.