XAML C # Resources - How to add a list of resources dynamically based on a folder

0

I am 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 dynamically added 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" />

Note the Key having a sequential numeric pattern. After that, to access and include them in the FlipView dynamically in the code behind, I make a new cachorrisse:

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 burst because it does not exist. I wanted to make this more malleable. I imagine something like when I add a new image in the Resources / Layouts folder the XAML already has a list of resources based on that folder, so consequently this new image will also be there. But I do not know how to create a list of resources or something like that. And in CodeBehind I'd like to be able to create foreach to fetch all these images. Being a collection, this should be much easier.

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

    
asked by anonymous 29.08.2016 / 22:31

0 answers