Where should I put a png file in Xamarin.Forms project?

Hi, I have a Xamarin.Forms project. Within this project I'm using CocosSharpView. In Cocossharp I'd like to use "CCSprite" and png file. How can I add it to the project.

I saw a few tutorials. Every tutorial was specific for a platform. James Montemagno did one for Android.

My question is, how, where I should add my png file in Xamarin.Forms project to make this work on every platform? In PCL? In every platform-specific project?

Thanks!

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Each platform has its own required location.
    What I do is keep the actual .png in a folder in the PCL, then add a link to the file in each of the platform projects. Not duplicates of the image, but a link. Then if managements decides to replace the red dot for a blue dot or whatever, I just change the image in the PCL (has to be the same name of course).

  • IshThomasIshThomas USMember ✭✭

    Thanks,
    Hmm.. can I ask a follow up question? How do you create this link?

    Thanks for help!

  • IshThomasIshThomas USMember ✭✭
    edited February 2017

    @ClintStLaurent Can you show me how to add those links or send some link how to do that?
    I've never done that before. I couldn't find single example with CocosSharpView on github: https://github.com/mono/cocos-sharp-samples

    I created new folder in PCL. Added "Existing item" - my png file. Set "Copy to output directory" to "Copy always"...

  • IshThomasIshThomas USMember ✭✭

    @ClintStLaurent Thanks! That helped me a lot! Although, I put a link into "Resources->drawable" in Android. It didn't work for me. I found out that image has to be in Assets->Content directory.

    Unfortunately I couldn't do in iOS. I tried in "Resources" folder, then I created a "Content" folder - but it didn't work. Do I have to set something to point, where png is in iOS?

    I'm using Xamarin.Forms with a common PCL library.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @IshThomas
    Very strange. Those are the specified folders and work fine in my projects.
    How are you trying to load the images?

    Xamarin guide to images:
    https://developer.xamarin.com/guides/xamarin-forms/working-with/images/
    As you can see, those are the expected/standard directories.

    Personally, I'd look into how you're accessing the images - compare it to the official Xamarin directions - and fix up your code until it works with the expected directories. Otherwise you probably can expect on-going unexpected issues and behaviors from doing things in an atypical manner.

  • IshThomasIshThomas USMember ✭✭
    edited February 2017

    Maybe it's different in CocosSharp?

    I'm using it like this:

            var ball = new CCSprite("redball");
            ball.ContentSize = new CCSize((float)circleSize - 2, (float)circleSize - 2);
            ball.Position = new CCPoint(rTestX, rTestY);
            layer.AddChild(ball);
    

    In this video:
    James Montemagno is building simple game (although it's in Xamarin.Android only, not Xamarin.Forms).

    19:41: He's inserting his images into Assets->Content, then
    21:20: He's explaining why Content.

    But he never said anything about iOS! Wrrr... So it's not clear to me where should images go in iOS. Interesting thing is, I didn't have to set "ContentRootDirectory = "Content" in Xamarin.Forms and it worked on Android.

  • IshThomasIshThomas USMember ✭✭

    A little update:
    I found this repository: https://github.com/mono/cocos-sharp-samples/tree/xamarin_forms/GoneBananas2
    In branch xamarin_forms they have working version of Xamarin.Forms.

    In my GamePage, I have view with ViewCreated event. In this event I added:

                var contentSearchPaths = new List<string>() { "Fonts", "Sounds", "Images", "Animations" };
                gameView.ContentManager.SearchPaths = contentSearchPaths;
    

    Changed catalog structure to:

    on Android: Assets->Content->Images
    on iOS: Content->Images

    put links to files there, and still.. it works on Android but it doesn't work on iOS. I have no idea what I should do.

    PS. The sample from github works and I can see images in iOS. So I've no idea what I'm missing.

  • TiagoRodriguesNesTiagoRodriguesNes USMember ✭✭

    Thanks a lot @ClintStLaurent works smoothly ;D

Sign In or Register to comment.