I am struggling to teach myself C#, and long googling has not turned up this answer:
I have a classes Rom, Game and Art. Rom.Game.Art is for images and is defined thus:
public class art
{
public Image Screen { get; set; }
public Image Marquis { get; set; }
public Image Logo { get; set; }
public art ()
{
BitmapImage Default_image = new BitmapImage();
Default_image.BeginInit();
Default_image.UriSource = new Uri(@"C:\Users\Major Major\Documents\Visual Studio 2013\Projects\Robin\Robin\images\bee_ace.png", UriKind.Absolute);
Default_image.EndInit();
this.Screen = new Image();
this.Screen.Source = Default_image;
this.Marquis = new Image();
this.Marquis.Source = Default_image;
this.Logo = new Image();
this.Logo.Source = Default_image;
}
}
I want to create an array of Roms, and populate the many nested properties and subclasses from a DataTable in a loop. Before I get into the loop, though, I just want to get a block of test code to work. The following works just fine:
InitializeComponent();
BitmapImage Image_0 = new BitmapImage();
Image_0.BeginInit();
Image_0.UriSource = new Uri(@"E:\Arcade\Art\Box\Intellivision\Congo Bongo.jpg", UriKind.Absolute);
Image_0.EndInit();
ROMS.Insert(0, new Rom());
ROMS[0].Name = "Congo Bongo";
ROMS[0].Game.Art.Screen.Source = Image_0;
ROMS[0].Game.Date = "1983";
ROMS[0].Game.Platform = "Intellivision";
ROM_list.ItemsSource = ROMS;
My complaint is that, if I put this in a loop, all of the images in the array will be the same--they will be the last image entered. It seems the expression "ROMS[0].Game.Art.Screen.Source = Image_0;" ties ROMS[0].Game.Art.Screen.Source to the variable Image_0, rather than just transferring the value (this seems like surprising behavior to me, but no worries). So that in order to keep populating ROMS[j].Game.Art.Screen.Source, I have to create an array of BitmapImages separate from the ROM array as a reference. What I'd prefer is to instantiate(?) the BitmapImages within the array, like this:
ROMS[0].Game.Art.Screen.Source = new BitmapImage();
InitializeComponent();
ROMS[0].Game.Art.Screen.Source.BeginInit();
ROMS[0].Game.Art.Screen.Source.UriSource = new Uri(@"E:\Arcade\Art\Box\Intellivision\Congo Bongo.jpg", UriKind.Absolute);
ROMS[0].Game.Art.Screen.Source.EndInit();
This doesn't work. Rom.Game.Art.Screen.Source is not a BitMapImage, and I cannot find the methods to assign an image to it.
So my questions, specifically: 1. Is there a way to assign an image source to ROMS[0].Game.Art.Screen.Source without creating a separate BitmapImage that lives on separately, or is that stupid?
Is there a smarter way to do what I'm trying to do?
Is there a reference to help me understand the relationships between the myriad image classes in C#?
Sorry for the wordy question, and thanks.
I think you should do some review about DataBinding. You will save more time.
You just need create something like the models:
ROM.Game.Art.Screen.Path ="meuPath.Jpg";
myItemSource = ROMS;
and in the end, do something like:
<ItemsControl x:Name="myItemSource">
<TextBlock Text="{Binding ROM.Game.Art.Screen.Name}"/>
<Image Source="{Binding ROM.Game.Art.Screen.Path,Converter=MyConverterImageToPath"/>
</ItemsControl>