Follow @PDSAInc March 2011 - Posts - Paul Sheriff's Blog for the Real World

Paul Sheriff's Blog for the Real World

This blog is to share my tips and tricks garnered over 25+ years in the IT industry

Paul's Favorites

March 2011 - Posts

Mirror Reflections with Silverlight Images

Adding images is very easy to do in Silverlight. All you do is add an <Image> element to your Silverlight user control with the Source property set to the path of where your image is located and that image will be displayed. So the following code will produce the image shown in Figure 1.

<Image Source="Computer_Cleaner.png" />

Figure 1: A Simple Silverlight Image 

Figure 1: A simple Silverlight image

A Mirror Reflection

However, just displaying a simple image is pretty plain. Through the magic of XAML you can make this image a little more interesting. One of the neat things we can do with XAML is to flip an image completely upside down by using a RenderTransform. By putting a ScaleTransform as your RenderTransform on a XAML element and setting the ScaleY property to minus one you can cause that XAML element to flip upside down. The following XAML will render the image shown in Figure 2.

<StackPanel>
  <Image Source="Computer_Cleaner.png"
          Name="theImage"
          Height="70"
          Width="50" />
  <Image Source="{Binding ElementName=theImage, Path=Source}"
          Height="{Binding ElementName=theImage, Path=Height}"
          Width="{Binding ElementName=theImage, Path=Width}"
          Opacity=".20"
          RenderTransformOrigin="0.0,0.15">
    <Image.RenderTransform>
      <ScaleTransform ScaleY="-1" />
    </Image.RenderTransform>
  </Image>
</StackPanel>

Figure 2: An image with a mirror reflection

Figure 2: An image with a mirror reflection

Now this XAML takes a little more explanation. First off there are now two image controls within a StackPanel control. The first image has is the same as shown in the first example, but we have set the Name property and an explicit Height and Width. The second image uses data binding to use the same Source, Height and Width as the first image.

To the second image are three additional properties added. First is the Opacity of 20%. This is what gives the second image the opaque, “reflection” look. A RenderTransform of a ScaleTransform with its ScaleY set to minus one to flip the image. The last property is an offset to make this image appear below the first image. This is done by setting the RenderTransformOrigin to “0,0.15”. There are two values separated by a comma in this origin property. They are the X and Y coordinates to set the center point of the render transform. Since we are not using ScaleX in the ScaleTransform, you only need to set the Y coordinate to a 15% offset of the center point. This is what moves the image a little below the first image.

Summary

Creating a mirror image of any control in XAML is as easy as adding a RenderTransform. Making reflections can spice up your user interface a little and make it not so drab. You can expand on this concept by adding a nice Border with an interesting background around the image. Put a bunch of these images together and you can use them as a nice menu system.

NOTE: You can download the complete sample code at my website. http://www.pdsa.com/downloads. Choose Tips & Tricks, then "Mirror Reflections with Silverlight Images" from the drop-down.

Good Luck with your Coding,
Paul Sheriff

** SPECIAL OFFER FOR MY BLOG READERS **
We frequently offer a FREE gift for readers of my blog. Visit http://www.pdsa.com/Event/Blog for your FREE gift!

 

Posted: Mar 23 2011, 10:44 AM by psheriff | with no comments
Filed under: ,
UserAppDataPath in WPF

In Windows Forms applications you were able to get to your user's roaming profile directory very easily using the Application.UserAppDataPath property. This folder allows you to store information for your program in a custom folder specifically for your program. The format of this directory looks like this:

C:\Users\YOUR NAME\AppData\Roaming\COMPANY NAME\APPLICATION NAME\APPLICATION VERSION

For example, on my Windows 7 64-bit system, this folder would look like this for a Windows Forms Application:

C:\Users\PSheriff\AppData\Roaming\PDSA, Inc.\WindowsFormsApplication1\1.0.0.0

For some reason Microsoft did not expose this property from the Application object of WPF applications. I guess they think that we don't need this property in WPF? Well, sometimes we still do need to get at this folder.

You have two choices on how to retrieve this property. Add a reference to the System.Windows.Forms.dll to your WPF application and use this property directly. Or, you can write your own method to build the same path. If you add a reference to the System.Windows.Forms.dll you will need to use System.Windows.Forms.Application.UserAppDataPath to access this property.

Create a GetUserAppDataPath Method in WPF

If you want to build this path you can do so with just a few method calls in WPF using Reflection. The code below shows this fairly simple method to retrieve the same folder as shown above.

C#
using System.Reflection;

public string GetUserAppDataPath()
{
  string path = string.Empty;
  Assembly assm;
  Type at;
  object[] r;

  // Get the .EXE assembly
  assm = Assembly.GetEntryAssembly();
  // Get a 'Type' of the AssemblyCompanyAttribute
  at = typeof(AssemblyCompanyAttribute);
  // Get a collection of custom attributes from the .EXE assembly
  r = assm.GetCustomAttributes(at, false);
  // Get the Company Attribute
  AssemblyCompanyAttribute ct =
                ((AssemblyCompanyAttribute)(r[0]));
  // Build the User App Data Path
  path = Environment.GetFolderPath(
              Environment.SpecialFolder.ApplicationData);
  path += @"\" + ct.Company;
  path += @"\" + assm.GetName().Version.ToString();

  return path;
}


Visual Basic
Public Function GetUserAppDataPath() As String
  Dim path As String = String.Empty
  Dim assm As Assembly
  Dim at As Type
  Dim r As Object()

  ' Get the .EXE assembly
  assm = Assembly.GetEntryAssembly()
  ' Get a 'Type' of the AssemblyCompanyAttribute
  at = GetType(AssemblyCompanyAttribute)
  ' Get a collection of custom attributes from the .EXE assembly
  r = assm.GetCustomAttributes(at, False)
  ' Get the Company Attribute
  Dim ct As AssemblyCompanyAttribute = _
                 DirectCast(r(0), AssemblyCompanyAttribute)
  ' Build the User App Data Path
  path = Environment.GetFolderPath( _
                Environment.SpecialFolder.ApplicationData)
  path &= "\" & ct.Company
  path &= "\" & assm.GetName().Version.ToString()

  Return path
End Function

Summary

Getting the User Application Data Path folder in WPF is fairly simple with just a few method calls using Reflection. Of course, there is absolutely no reason you cannot just add a reference to the System.Windows.Forms.dll to your WPF application and use that Application object. After all, System.Windows.Forms.dll is a part of the .NET Framework and can be used from WPF with no issues at all.

NOTE: Visit http://www.pdsa.com/downloads to get more tips and tricks like this one.

Good Luck with your Coding,
Paul Sheriff

** SPECIAL OFFER FOR MY BLOG READERS **
We frequently offer a FREE gift for readers of my blog. Visit http://www.pdsa.com/Event/Blog for your FREE gift!

DevConnections new "Fundamentals" Track!

Hi All,

I am now the new Track Chair for the "Fundamentals" track at DevConnections. I know many of my readers feel overwhelmed by all of the "advanced" topics out there. The folks at the DevConnections conference realized that too and have added many new sessions that help programmers that are in the beginning to intermediate stage get up to speed on all the new technology that is coming out so fast. I will be presenting a whole day long workshop at the DevConnections conference in Orlando on March 27th entitled "Essential Business Desktop Programming with .NET". In addition I will be presenting the following sessions in the Fundamentals Track.

  • MVVM Made Simple
  • Unit Testing Basics and Architecting Your Application for Unit Testing
  • Data Binding from A-Z in Silverlight
  • From Zero to Windows Phone 7 in 75 Minutes
I hope I will see you there! Join me at DevConnections @devconnections in Orlando March 27-30.   Save $200 use discount code DevCon1 Register today at bit.ly/fIZjXO
Posted: Mar 09 2011, 01:18 PM by psheriff | with no comments
Filed under: ,
More Posts