Reading Resources
// Get single resource
StreamResourceInfo sri = Application.GetResourceStream(
new Uri("images/Hammer.gif", UriKind.Relative));
Get list of resourses...
Assembly myassembly = Assembly.GetAssembly(this.GetType());
string rName = myassembly.GetName().Name + ".g";
ResourceManager rm = new ResourceManager(rName, myassembly);
using (ResourceSet rSet =
rm.GetResourceSet(CultureInfo.CurrentCulture, true, true))
{
foreach (DictionaryEntry r in rSet)
{
MessageBox.Show(r.Key.ToString());
}
}
Convention:
pack://application:,,,/AssemblyName;component/ResourceName
if your image is embedded in a referenced assembly:
resource in other library or dll
img.Source = new BitmapImage(
new Uri("pack://application:,,,/ImagesallLib;component/images/hammer.png"));
img.Source = new BitmapImage(
new Uri("ImageLibrary;component/images/hammer.jpg", UriKind.Relative));
with version:
img.Source = new BitmapImage(
new Uri("ImageLibrary;v1.25;component/images/hammer.jpg.jpg",
UriKind.Relative));
with version: and strong name (publik key):
img.Source = new BitmapImage(
new Uri("ImageLibrary;v1.25;ac32aa7f5bdqbqbc;component/images/hammer.jpg.jpg",
UriKind.Relative));
object resources are different from assembly resources
assembly resource is binary data and is embedded in compiled assembly.
images strings etc..
object resource, is a .NET object that we refer in other places
Application, window or any and every element can maintain resources(as below..)
<Button>
<Button.Resources>
<ImageBrush x:Key="TileBrush"
TileMode="Tile"
ViewportUnits="Absolute"
Viewport="0 0 32 32"
ImageSource="/IMG_1415.jpg"
Opacity="0.3">
</ImageBrush>
</Button.Resources>
</Button>
The following content can exist in this button
<StackPanel Orientation="Horizontal">
<Rectangle Width="20" Height="40"
Fill="Red" />
<Button Background="{StaticResource TileBrush}"
Margin="5"
Padding="5"
FontWeight="Bold"
FontSize="14">
A Tiled Button
</Button>
<Button Background="{DynamicResource TileBrush}"
Margin="5"
Padding="5"
FontWeight="Bold"
FontSize="14">
A Tiled Button
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Rectangle Width="20" Height="40"
Fill="Red" />
<Button Background="{StaticResource TileBrush}"
Margin="5"
Padding="5"
FontWeight="Bold"
FontSize="14">
A Tiled Button
</Button>
<Button Background="{DynamicResource TileBrush}"
Margin="5"
Padding="5"
FontWeight="Bold"
FontSize="14">
A Tiled Button
</Button>
</StackPanel>
Both inner buttons work except that staticresource expects the resource available before parser reads this..(above it in the XAML)
Dynamicresource word ensures updating the brush whenever actual source resource changes. (Staticresource button will not change -- This is an overhead too ) use such resources diligently