Setting image source in only in runtime (Without define it in the XAML)
Hi,
I'm trying to create XAML page with several images that will only be set in the code during runtime. Here is a sample of image tag from my XAML -
<Image Name="CloseAppWindow" Width="454" Height="256" Canvas.Top="45" Canvas.Left="24"></Image>
As you can see I did not specify the source for it since I want to do it through code. However the resources of the images are kept in a different project and no matter what I do to send them as parameters the XAML window is drawn without them.
Any ideas will be great,
Shak
Re: Setting image source in only in runtime (Without define it in the XAML)
You can fetch the image from other project using reflection.
Note that this will work only when the images are embedded in the project dll.
Re: Setting image source in only in runtime (Without define it in the XAML)
Hi,
Thanks for your replay.
Can you please explain further how to do the reflection?
1 Attachment(s)
Re: Setting image source in only in runtime (Without define it in the XAML)
Quote:
Originally Posted by
shak_pg
Hi,
Thanks for your replay.
Can you please explain further how to do the reflection?
Look into the attached sample POC.
It is developed in VS2010; so if you cant opn the solution, try looking into the files using the VS version that you have.
I hope this will help.
Re: Setting image source in only in runtime (Without define it in the XAML)
Thanks for the example but I did not understand how your example can help me.
I was talking about XAML(WPF) that will define images in the XAML but the source to that image will be decided upon runtime.
:-(
1 Attachment(s)
Re: Setting image source in only in runtime (Without define it in the XAML)
Here have a look into the below code:
Xaml contains the image tag without source.
Code:
<Window x:Class="FetchResource_SampleApp_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image x:Name="closeAppWindow" Width="454" Height="256"></Image> </Grid>
</Window>
In the code behind, do the following:
Code:
System.Reflection.Assembly otherProject;
otherProject = System.Reflection.Assembly.GetAssembly(typeof(ResourceLib.Class1));
System.IO.Stream streamFile = otherProject.GetManifestResourceStream("ResourceLib.Images.ExpressionBlend.JPG");
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = streamFile;
bitmap.EndInit();
closeAppWindow.Source = bitmap;
Do not forget to check the attachment :D
Re: Setting image source in only in runtime (Without define it in the XAML)