locked
cant show images RRS feed

  • Question

  • User389721 posted

    I'm using xamarin forms, and im trying to add an image to my main activity, and for some reason I just can't. im using visual studio for windows , and my project is .net cross-platform.

    Monday, October 14, 2019 8:33 PM

Answers

  • User379860 posted

    @maorts14 First of all, create a converter for converting from string to ResourceImageSourceuse like following code.

       [ContentProperty (nameof(Source))]
       public class ImageResourceExtension : IMarkupExtension
       {
        public string Source { get; set; }
    
        public object ProvideValue (IServiceProvider serviceProvider)
      {
       if (Source == null)
       {
          return null;
       }
    
       // Do your translation lookup here, using whatever method you require
       var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
    
        return imageSource;
        }
       }
    

    In the end use it in your code.

      <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:App57;assembly=App57"
             mc:Ignorable="d"
             x:Class="App57.MainPage">
    
    <StackLayout>
    
        <Image Source="{local:ImageResource App57.images.microsoft.jpg}"/>
    </StackLayout>
    
     </ContentPage>
    

    Here is running screenshot.

    If you want to know more details about it, you can refer to this link. https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=macos#embedded-images

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 15, 2019 1:48 AM