Managing Embedded Resources : Resources « Containers « Silverlight






Managing Embedded Resources

Managing Embedded Resources
   

<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>
  <StackPanel>
    <ListBox x:Name="ResourceNames" Background="Transparent" HorizontalAlignment="Stretch" Margin="4,4,15,4" SelectionChanged="ResourceNames_SelectionChanged">
      <ListBox.Foreground>
        <SolidColorBrush Color="#FFD18726"/>
      </ListBox.Foreground>
      <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
          <Setter Property="Background" Value="Transparent"/>
        </Style>
      </ListBox.ItemContainerStyle>
    </ListBox>
    <Button x:Name="RetrieveResourceNames" Content="Retrieve Resource Names" d:LayoutOverrides="VerticalAlignment, Height" Click="RetrieveResourceNames_Click"/>
    <TextBlock HorizontalAlignment="Stretch" Margin="53,0,74,4" VerticalAlignment="Bottom" Text="Select a Resource to Display" TextWrapping="Wrap" Foreground="#FFFFFFFF" Height="22"/>
    <Border Margin="30,-15,30,35" HorizontalAlignment="Stretch" BorderBrush="#FF000000" x:Name="ImageBorder" RenderTransformOrigin="0.5,0.5" Visibility="Collapsed" Height="310" VerticalAlignment="Stretch" d:LayoutOverrides="Height">
      <Border.Background>
        <SolidColorBrush Color="#FFD28826"/>
      </Border.Background>
      <Border.RenderTransform>
        <TransformGroup>
          <ScaleTransform/>
          <SkewTransform AngleX="5" AngleY="5"/>
          <RotateTransform/>
          <TranslateTransform/>
        </TransformGroup>
      </Border.RenderTransform>
      <Image x:Name="ImageDisplay" Margin="5,5,5,5" Width="400" Height="300" OpacityMask="#FF000000" />
    </Border>
  </StackPanel>
</UserControl>

//File: Page.xaml.cs
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace SilverlightApplication3
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
    }

    private void RetrieveResourceNames_Click(object sender, RoutedEventArgs e)
    {
      Assembly app = Assembly.GetExecutingAssembly();
      string[] resources = app.GetManifestResourceNames();
      ResourceNames.Items.Clear();
      foreach (string s in resources)
      {
        ResourceNames.Items.Add(s);
      }
    }

    private void ResourceNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      if (!(ResourceNames.SelectedItem.ToString().Contains("resources")))
      {
        Assembly app = Assembly.GetExecutingAssembly();
        using (Stream stream = app.GetManifestResourceStream(ResourceNames.SelectedItem.ToString()))
        {
          BitmapImage bImage = new BitmapImage();
          bImage.SetSource(stream);
          ImageDisplay.Source = bImage;
          ImageBorder.Visibility = Visibility.Visible;
        }
      }
    }
  }
}

   
    
    
  








Related examples in the same category

1.Adding a Your own data class as ResourcesAdding a Your own data class as Resources
2.Managing XAML ResourcesManaging XAML Resources
3.Customizing a Control's Basic AppearanceCustomizing a Control's Basic Appearance
4.Font Size Resources
5.Logical ResourcesLogical Resources
6.Retrieving assembly manifest resources
7.Resource Lookup
8.Use Resources.Add to add static resouce from code
9.Reference resource in codeReference resource in code
10.Resource hierarchyResource hierarchy
11.LinearGradientBrush as ResourceLinearGradientBrush as Resource
12.A Uri accessing a relative loose resource
13.A Uri accessing a relative loose resource in a subdirectory
14.An absolute Uri accessing a loose resource
15.An absolute Uri accessing a loose resource in a subdirectory
16.The basic syntax and usage of a resourceThe basic syntax and usage of a resource
17.Referencing a resource at design time. Notice the use of the StaticResource keyword.Referencing a resource at design time. Notice the use of the StaticResource keyword.
18.An invalid use of a resource at design time
19.Use external resource