Here's a screen shot of my demo application showing different ways of accessing resources.
Some images are populated using Xaml and others using standard C# mechanisms.
Here's the structure of the application, note the two different images included in the Assets folder. One is set to the newer "Resource" content-type and the other to the old "EmbeddedResource". The image in the DifferentAssembly is also a "Resource". (Prefer Resource to the old EmbeddedResouce).
<Window
x:Class="ResourceAccessExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350"
Title="MainWindow"
Width="525">
<ScrollViewer>
<StackPanel>
<Border
BorderBrush="Black"
BorderThickness="1"
Margin="10"
Padding="10">
<StackPanel>
<Image
MaxHeight="75"
Source="pack://application:,,,/assets/ChessMce.png"
/>
<TextBlock Text="Pack Url into same assembly" />
</StackPanel>
</Border>
<Border
BorderBrush="Black"
BorderThickness="1"
Margin="10"
Padding="10">
<StackPanel>
<Image
MaxHeight="75"
Source="pack://application:,,,/DifferentAssembly;component/UserGroup.png"
/>
<TextBlock Text="Pack Url into different assembly" />
</StackPanel>
</Border>
<Border
BorderBrush="Black"
BorderThickness="1"
Margin="10"
Padding="10">
<StackPanel>
<Image
MaxHeight="75"
Source="assets/ChessMce.png"
/>
<TextBlock Text="Relative Url into same assembly" />
</StackPanel>
</Border>
<Border
BorderBrush="Black"
BorderThickness="1"
Margin="10"
Padding="10">
<StackPanel>
<Image
x:Name="Image2"
MaxHeight="75"
/>
<TextBlock Text="Pulled from Resource Manifest using code Application static helper ('Resource')" />
</StackPanel>
</Border>
<Border
BorderBrush="Black"
BorderThickness="1"
Margin="10"
Padding="10">
<StackPanel>
<Image
x:Name="Image3"
MaxHeight="75"
/>
<TextBlock Text="Pulled from Resource Manifest using standard .NET Resource classes ('Resource')" />
</StackPanel>
</Border>
<Border
BorderBrush="Black"
BorderThickness="1"
Margin="10"
Padding="10">
<StackPanel>
<Image
x:Name="Image4"
MaxHeight="75"
/>
<TextBlock Text="Pulled from an old EmbeddedResource using standard .NET Resource classes ('Resource')" />
</StackPanel>
</Border>
</StackPanel>
</ScrollViewer>
</Window>
And the C# Code behind that populates the last three images using standard C# coding techniques:
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object s, RoutedEventArgs e)
{
// Pull using the Application object (wpf only)
var streamResourceInfo2 = Application.GetResourceStream(new Uri("assets/ChessMce.png", UriKind.Relative));
if (streamResourceInfo2 != null)
{
var image = new BitmapImage();
image.BeginInit();
image.StreamSource = streamResourceInfo2.Stream;
image.EndInit();
this.Image2.Source = image;
}
// Using standard .NET resource classes.
var assem = Assembly.GetEntryAssembly();
var resourceManager = new ResourceManager(assem.GetName().Name + ".g", assem);
using (ResourceSet set = resourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true))
{
// Can loop thru them
foreach (DictionaryEntry pair in set)
{
Debug.WriteLine(pair.Key.ToString());
}
// Access an item directly
var image = new BitmapImage();
image.BeginInit();
image.StreamSource = (Stream)set.GetObject("assets/ChessMce.png", true);
image.EndInit();
this.Image3.Source = image;
}
// Using an old Embedded Resource
// this line of code is useful to figure out the name Vs has given the resource! The name is case sensitive.
// var names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ResourceAccessExample.Assets.FreeCellMCE.png"))
{
var image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
this.Image4.Source = image;
}
}
}
Brain capacity recycled...
No comments:
Post a Comment