XAML C# Resources - How to add a list of dynamically based Resources Image in a folder

Asked

Viewed 148 times

0

I’m making a FlipView using MahApps.Metro.

I have a list of images that would be passed to this flipview. It works, but I’m making them add dynamically in a way that I think is incorrect:

    <Image x:Key="Fdl1" Source="Resources/Layouts/1.bmp" />
    <Image x:Key="Fdl2" Source="Resources/Layouts/2.bmp" />
    <Image x:Key="Fdl3" Source="Resources/Layouts/3.bmp" />
    <Image x:Key="Fdl4" Source="Resources/Layouts/4.bmp" />
    <Image x:Key="Fdl5" Source="Resources/Layouts/5.bmp" />
    <Image x:Key="Fdl6" Source="Resources/Layouts/6.bmp" />
    <Image x:Key="Fdl7" Source="Resources/Layouts/7.bmp" />
    <Image x:Key="Fdl8" Source="Resources/Layouts/8.bmp" />
    <Image x:Key="Fdl9" Source="Resources/Layouts/9.bmp" />
    <Image x:Key="Fdl10" Source="Resources/Layouts/10.bmp" />
    <Image x:Key="Fdl11" Source="Resources/Layouts/11.bmp" />
    <Image x:Key="Fdl12" Source="Resources/Layouts/12.bmp" />
    <Image x:Key="Fdl13" Source="Resources/Layouts/13.bmp" />
    <Image x:Key="Fdl14" Source="Resources/Layouts/14.bmp" />
    <Image x:Key="Fdl15" Source="Resources/Layouts/15.bmp" />
    <Image x:Key="Fdl16" Source="Resources/Layouts/16.bmp" />

Look at Key having a sequential numerical pattern. After this, to access them and include in Flipview dynamically in the Behind code, I make a new puppy:

for (int i = 1; i <= FACTORY_IMG_TOTAL_RESOURCES; i++)
        {
            VisualBrush vb = new VisualBrush();
            vb.Visual = (Visual)this.Resources["Fdl" + i]; // esta é a parte que 
                                                           // me escondo debaixo da mesa
            Rectangle rect = new Rectangle();
            rect.Width = 300;
            rect.Height = 188.51;
            rect.Fill = vb;
            Grid g = new Grid();
            g.Children.Add(rect);
            FpImg.Items.Add(g);
        }

As you can see, if I delete one of the images it should already burst Exception by not existing. I wanted to make it more pliable. I imagine something like when I add a new image in the Resources/Layouts folder XAML already have a list of resources based on that folder, so consequently this new image will also be there. But I don’t know how to create a list of resources or something like that. And in the CodeBehind I wish I could create a foreach to fetch all these images. Being a collection, this should be much easier.

It will be great for my learning, because this same problem I’m having in some other cases.

  • How did you notice these Resources are static, that is, there is no way to manipulate them, a solution would be to manipulate the addition/removal of files through files json or xml. @Robson-ribeiro-faxas

  • I solved in the most traditional way, I added the data in the database.

  • However, the loading of the images on the selection screen was slow due to the amount of data, so I created a Thread that searches the images in the database and implements the progress of a progressibar. Thus the execution is not locked and the process is loaded normally. Using json or xml is an interesting possibility too, I will include this possibility next time you need to load many Resources.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.