Visual Studio 2017 - Where is ResourceId after making an image an Embedded Resource

I'm not sure if I'm posting to the right area, so please tell me if I haven't.

I'm currently trying to develop a Xamarin.Forms app for use on Android and iOS and are currently trying to display an image as a background.
I'm following a course and it states that when you change the properties of an image to "Embedded Resource", it should show the 'ResourceId'.

When changing the image to "Embedded Resource", nothing in the properties pane changes at all to show ResourceID.
When doing the same thing in Visual Studio for Mac, it shows the ResourceID (note: I can set the resource ID in Visual Studio for Mac and it will compile properly on the Windows VS, but it still doesn't show the ResourceID.

I've tried multiple projects, and can't get it to work. Am I maybe missing something?

Thanks heaps.

Answers

  • AndreiMungiuAndreiMungiu DKMember

    I have exactly the same problem, please help!

  • Been over a month and no solution yet... Gives me little hope it would do anytime soon... :neutral:

  • AyalaFuksAyalaFuks USMember

    I have exactly the same issus please help me!!!!!

  • fo0fo0fo0fo0 Member ✭✭

    I have also the same issue .Please hep us :(

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @Beau-DeanAdams and others, Are you trying to set the Bacground image at Page level?

  • fo0fo0fo0fo0 Member ✭✭

    If I change the image file property Build Action to 'Embedded Resource' ,the ResourceID doesn't appear .
    This is the screenshot of my property pane

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    I have only set the BuildAction to AndroidResource or BundledResource and then just it at page level as,

    <ContentPage ... ... ...
          BackgroundImage="MyImage.png" >
    
    </ContentPage>
    

    I have never used any ResourceId. Can you give excerpts from the course where this was used?

  • fo0fo0fo0fo0 Member ✭✭

    Thank you for your kind response.

    I'm using this method in code behind :
    public Images ()
    {
    InitializeComponent ();
    //here must be ResourceID but it doesn't work with me
    image.Source = ImageSource.FromResource("MyXamarine_2.Images.background.jpg");

        }
    

    On page level

        <StackLayout>
            <Image Aspect="AspectFill" x:Name="image"/>
        </StackLayout>
    
  • Og73Og73 Member

    Has this been resolved by anyone, I'm having the same issue...

  • This is happening to me too and it is driving me mad. Please someone help.

  • Found a fix!
    Not sure if it's the right one, but it's working for me. Described here: docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=vswin

    why it happens
    Basically, resources in the "regular" folder of a cross-platform app get sort of re-created inside the folder of the currently active platform, so any reference to a resource path that's not platform-specific might not work. This may be why they don't show a resourceID for images in the main or "regular" folder--because it's dynamically assigned, and if there's a value listed there, and the programmer hard-codes before launch, it may not work.

    solution
    They give the solution on the referenced page, but here it is specifically applied to my image needs. Basically you use a "compiler directive" (which I didn't know about) to dynamically assign the prefix for the resource ID. Here's how it works for me:

    public partial class MainPage : ContentPage
    {
        #if __IOS__
            public string ResourcePrefix = "MyApp.iOS.";
        #elif __ANDROID__
            private string ResourcePrefix = "MyApp.Droid.";
        #else
            private string ResourcePrefix = "MyApp.";  //note: this part isn't working to get the image in the preview
        #endif
    
        private string ImageFileName = "flower.jpg";
    
        public MainPage()
        {
            InitializeComponent();
            imageNameDefinedInXaml.Source = ImageSource.FromResource(ResourcePrefix + ImageFileName);
        }
    }
    

    Two notes:

    • The compiler directive if-else flow ends with just the app name, my attempt to get the image to appear in the Xaml Preview window. It didn't work. Just included here in case anyone else can figure it out.
    • My file is actually in a folder called "Images," so according to the referenced documentation page, the variable ImageFileName should require that as a prefix, and as such, be defined like this: "Images.flower.jpg" But in my experience (as of late May 2018), adding that prefix makes it fail. Just the raw filename seems to work no matter the sub-directory. YMMV.
  • That fix doesn't work!

    I neglected to test it on iOS and Android. Shucks.

    This however did work for me: https://forums.xamarin.com/discussion/17953/helpful-shared-project-image-information

  • UGH now that one didn't work.

    This is working for me right now:

    using System.Linq;
    using System.Reflection;
    using System.Diagnostics;
    using System;
    
    namespace Helpers
    {
        public class EmbeddedSourceror
        {
            public static Xamarin.Forms.ImageSource SourceFor(string pclFilePathInResourceFormat)
            {
                var resources = typeof(EmbeddedSourceror).GetTypeInfo().Assembly.GetManifestResourceNames();
                var resourceName = resources.Single(r => r.EndsWith(pclFilePathInResourceFormat, StringComparison.OrdinalIgnoreCase));
                Debug.WriteLine("EmbeddedSourceror: resourceName string is " + resourceName);
    
                return Xamarin.Forms.ImageSource.FromResource(resourceName);
            }
        }
    }
    

    This is an amalgalm of code found in the above link as well as this post: https://forums.xamarin.com/discussion/73011/xamarin-forms-get-path-of-embedded-file

Sign In or Register to comment.