0

I have a method to assign six images to each player as follows:

public void AddCards(Player one, Player two) { one.Images.Add(Card1Player); one.Images.Add(Card2Player); one.Images.Add(Card3Player); one.Images.Add(Card4Player); one.Images.Add(Card5Player); one.Images.Add(Card6Player); two.Images.Add(Card1Computer); two.Images.Add(Card2Computer); two.Images.Add(Card3Computer); two.Images.Add(Card4Computer); two.Images.Add(Card5Computer); two.Images.Add(Card6Computer); } 

My xaml file contains these images:

<Image Name="Card1Player" HorizontalAlignment="Left" Height="97" Margin="259,249,0,0" VerticalAlignment="Top" Width="71" /> <Image Name="Card2Player" HorizontalAlignment="Left" Height="97" Margin="369,249,0,0" VerticalAlignment="Top" Width="71" /> <Image Name="Card3Player" HorizontalAlignment="Left" Height="97" Margin="158,249,0,0" VerticalAlignment="Top" Width="71" /> <Image Name="Card4Player" HorizontalAlignment="Left" Height="97" Margin="478,249,0,0" VerticalAlignment="Top" Width="71" /> <Image Name="Card5Player" HorizontalAlignment="Left" Height="97" Margin="59,249,0,0" VerticalAlignment="Top" Width="71" /> <Image Name="Card6Player" HorizontalAlignment="Left" Height="97" Margin="581,249,0,0" VerticalAlignment="Top" Width="71" /> 

Same for the other six cards.

Is it possible to do this in a better way? I was thinking about something like creating a parent for each six Images and then getting the images from that. I don't know if this is possible.

I could also change the AddCards method to something like this:

public void AddCards(Player one, Player two) { one.Images.AddRange(new List<Image>() { Card1Player, Card2Player}); two.Images.AddRange(new List<Image>() { Card1Computer, Card2Computer}); } 

It would be neat to get all images from the xaml without specifying all those names. Thats why I was thinking about a container / parent for those images.

1
  • MVVM does this better AFAIK. Try to google some article on it. good luck Commented Jul 2, 2014 at 15:50

1 Answer 1

1

Try to put it in Grid, specify a name for it e.g. PlayerImagesGrid than you may use something like this

 for (var i = 0; i < VisualTreeHelper.GetChildrenCount(PlayerImagesGrid); i++) { var child = VisualTreeHelper.GetChild(PlayerImagesGrid, i); var image = child as Image; if (image != null) { one.Images.Add(image); } } 

It's not so neat but you need not worry about extensibility. Also you may remove images names if you do not use them anywhere else.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.