Home > Software engineering >  Xamarin load pictures with c# for creating a list
Xamarin load pictures with c# for creating a list

Time:06-20

i would like to load a list of some Pictures.

I dunno why its so hard to load a pic with c# Code Ive tried with many paths (see screenshots); All my Pics got the build action "embedded resource"

Code

Load a picture with xaml is no problem and there is no problem to load a picture by imageCell I can add to my stackpanel (stackkkk) the text "Hello" but not a image :(

Here is the xaml: Xaml

        var image = new Image { Source ="tattrank.images.indexPic.jpg"};
        var image2 = new Image { Source = "images.indexPic.jpg" };
        var image3 = new Image { Source = "/images.indexPic.jpg" };
        var image4 = new Image { Source = "indexPic.jpg" };

        Label lbl = new Label();
        lbl.Text = "Hello";

        stackkk.Children.Add(img);
        stackkk.Children.Add(lbl);
        stackkk.Children.Add(image);
        stackkk.Children.Add(image2);
        stackkk.Children.Add(image3);
        stackkk.Children.Add(image4);
        stackkk.Children.Add(lbl);

This is the Xaml Code:

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="tattrank.MainPage"
         xmlns:localPic="clr-namespace:tattrank">

<ScrollView>
    <StackLayout x:Name="stackkk">
    <Image Source="{localPic:image tattrank.images.indexPic.jpg}" HeightRequest="250"/>
    <Label Text="DIndexPage" FontSize="Title" Padding="30,10,30,10"/>
        <Label FontSize="16" Padding="30,24,30,0"/>
        <TableView Intent="Settings">
            <TableRoot x:Name="tbRoot">
                <TableSection Title="XamlCode" x:Name="TbSection">
                    <SwitchCell Text="New Voice Mail" />
                    <SwitchCell Text="New Mail" On="true" />
                </TableSection>
            </TableRoot>
        </TableView>
    </StackLayout>
</ScrollView>

</ContentPage>

CodePudding user response:

according to the docs, to load an image from an embedded resource

Image embeddedImage = new Image
{
    Source = ImageSource.FromResource("WorkingWithImages.beach.jpg", typeof(MyClass).GetTypeInfo().Assembly)
};

where MyClass is the name of a class that is located in the same assembly as the images

  • Related