question

DarylV avatar image
0 Votes"
DarylV asked KyleWang-MSFT commented

Image in List

Hello,

I am trying to include images in my list that will be bound to the page. The intent is when the user hits next on the interface the new image will come on with the new text. The images are embeded and are in their own image folder. I have included ImageA (line 27) as the marker for the image I am trying to include but it is not working:

 public class ClassA
     {
         public string TitleText { get; internal set; }
         public string EngText { get; internal set; }
         public string ItText { get; internal set; }
         public string PageNo { get; internal set; }
         public string ImageA { get; internal set; }
     }
    
     public partial class ClassB : ContentPage
     {
         List<ClassA> ClassA { get; set; }
         int ndx = 0;
    
         public ClassB()
         {
             InitializeComponent();
    
             ClassA = new List<ClassA>
             {
                 new ClassA
                 {
                     TitleText = "Title Text",
                     EngText = "ENGtext",
                     ItText = "ITtext",
                     PageNo = "1/4",
                     ImageA = "local:ImageResource Project.Images.ClassB.Image.png"
                 },


Many thanks for your help,

dotnet-csharpdotnet-xamarinforms
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can you post the part of your XAML file where you're binding the image?

0 Votes 0 ·

Here is the section I will be binding the image to. Note the text is bound with no problem.

 <ScrollView VerticalOptions="FillAndExpand">
                 <StackLayout>
                     <Label Text="{Binding EngText}" 
                            Style="{StaticResource Key=TextLines}"/>
    
                     <Image Source="{Binding ImageA}" Style="{StaticResource Key=Size1}"/>
    
                     <Label Text="{Binding ItText}" 
                            Style="{StaticResource Key=TextLines}"/>
    
                 </StackLayout>
             </ScrollView>
0 Votes 0 ·

If you add this to your program entry point can you see the resource you're referencing?:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#debug-code

0 Votes 0 ·
Show more comments

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered KyleWang-MSFT commented

Hi DarylV,

Welcome to our Microsoft Q&A platform!

According to the documentation: Xamarin.Forms Basic Bindings, we know that the Binding is followed by a property. So you can't add a "local:ImageResource" string after Binding.

As a workaround, you can set the image resource as follows.
xaml

 <Image x:Name="MyImage" />

xaml.cs

 MyImage.Source = ImageSource.FromResource("WorkingWithImages.beach.jpg", typeof(EmbeddedImagesXaml).GetTypeInfo().Assembly);

Regards,
Kyle


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

can I add this into the list mentioned above?

0 Votes 0 ·

@DarylV Sure, you can store string "WorkingWithImages.beach.jpg" to ImageA property. And set image source as follows.

 MyImage.Source = ImageSource.FromResource(ClassA[0].ImageA, typeof(EmbeddedImagesXaml).GetTypeInfo().Assembly);
1 Vote 1 ·