When the Image's Source property is set the following way, the picture is taken from /Images/down.png
.
How would I do the same thing programmatically?
<Image x:Name="myImg" Source="/MyProject;component/Images/down.png" />
The following would not work, since Image.Source property is not of a string type.
myImg.Source = "/MyProject;component/Images/down.png";
Using setBackgroundResource() method: myImgView. setBackgroundResource(R.
The background will stretch according to the length given by the ImageView component, and SRC will hold the size of the original image without stretching. SRC is the picture content (foreground), BG is the background, can be used at the same time.
If you don't want to set image to xml file and show image programmatically then you need to use this method. For E.g. You set image in xml which showing ON Switch now when you click ON Button its should be set to OFF Switch so in that case you can set using this setImageResource() programmatically.
Try this:
BitmapImage image = new BitmapImage(new Uri("/MyProject;component/Images/down.png", UriKind.Relative));
myImg.Source = new BitmapImage(new Uri(@"component/Images/down.png", UriKind.RelativeOrAbsolute));
Don't forget to set Build Action to "Content", and Copy to output directory to "Always".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With