Xamarin studio folder structure issue in iOS project

最后都变了- 提交于 2019-12-10 12:41:08

问题


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.


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/16938250/xamarin-studio-folder-structure-issue-in-ios-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!