|
Changing/Swapping Existing Images Dynamically in Silverlight (Silverlight) |
|
|
Written by: Justin Rich
Changing/Swapping Existing Images Dynamically in SilverlightWithin Silverlight, it's useful to be able to change or swap the image source of an image already set on your XAML page for multiple purposes. I for one needed to accomplish this because I'm doing some game programming that involves me putting a different armor set on a fighting warrior depending on which armor is equipped. To do this, you can go ahead and set a XAML image within your page:
<Image x:Name="myHead" Source="images/Head1.png"></Image>
From this point, within the backend code, you would think that you could simply change the Source property of that object, but you can't. The Source property is actually a System.Windows.Media element that is being pre-rendered in your XAML build for you automatically. To change the source of your file, you actually need to do it like this:
Dim myUri As New Uri("images/Head2.png", UriKind.Relative) Dim img As ImageSource = New System.Windows.Media.Imaging.BitmapImage(myUri) myHead.SetValue(Image.SourceProperty, img)
In this, we're using the URI object to tell the system where to get this resource from, setting it as a new image source and then using the SetValue property to apply the image SourceProperty to the ImageSource. This will dynamically allow you to change the image used whenever and as often as you like. Just keep in mind that the link to the image is to the resource in your Silverlight Project and does not reference a uri to an external location.
Content Keywords: Image Swapping, Swap Images, Silverlight, VB.Net, C#.Net |
|
|
|
|
|