Archive for August, 2009

Finding the Parent

I didn’t realise that getting to the parent FrameworkElement was so easy. This code came from the Silverlight.net Forum, I like to link but I’ve lost the post.

GetParent((FrameworkElement)this, typeof(Grid));
private FrameworkElement GetParent(FrameworkElement child, Type targetType)
        {
            object parent = child.Parent;
            if (parent != null)
            {
                if (parent.GetType() == targetType)
                {
                    return (FrameworkElement)parent;
                }
                else
                {
                    return GetParent((FrameworkElement)parent, targetType);
                }
            }
            return null;
        }

A better way to find the parent using the VisualTreeHelper found on the Silverlight forums:

public static class ControlFinder
    {
        public static T FindParent<T>(UIElement control) where T: UIElement
        {
            UIElement p = VisualTreeHelper.GetParent(control) as UIElement;
            if (p != null)
            {
                if (p is T)
                    return p as T;
                else
                    return ControlFinder.FindParent<T>(p);
            }
            return null;
        }
}

Page p = ControlFinder.FindParent<Page>(CurrentControl);

if(p != null)

{

      // here is your Page Control that in the parent chain 

}

Create a Storyboard Programatically

I’ve just created my first Storyboard outside of Blend so I thought I break down the code and save it for reference

            // Create a duration of .5 seconds.
            Duration duration = new Duration(TimeSpan.FromMilliseconds(500));

            //Create two DoubleAnimationsUsingKeyFrame and set their properties.
            DoubleAnimationUsingKeyFrames myDoubleAnimation1 = new DoubleAnimationUsingKeyFrames();
            myDoubleAnimation1.Duration = duration;

            //Create an EasingDoubleKeyFrame
            EasingDoubleKeyFrame WidthChange = new EasingDoubleKeyFrame();
            WidthChange.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
            WidthChange.Value = DblHeight;
            CircleEase EsEase = new CircleEase();
            EsEase.EasingMode = EasingMode.EaseInOut;
            WidthChange.EasingFunction = EsEase;
            myDoubleAnimation1.KeyFrames.Add(WidthChange);

            Storyboard sb = new Storyboard();
            sb.Duration = duration;

            sb.Children.Add(myDoubleAnimation1);

            //I don't know why but Targets are set with a static method in the Storyboard class
            Storyboard.SetTarget(myDoubleAnimation1, AnimationTarget);
            Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(FrameworkElement.Width)"));

            return sb;

C# in minutes

This is a great little reference for anyone learning C#

Brief explanation and an example is all you need most of the time.

Formula for Isometric Vectors

I just read a great tutorial about creating isometric vectors which had a nice simple formula for creating them.

Scale the height by 86.062%
Shear by -30° or +30° depending on the direction of the perspective
Rotate by -30° or +30° depending on the direction of the perspective

Intuos tablet (Mrk 1) and Windows 7

I’ve just spent the last 3 hours getting my tablet working with windows 7 through a USB to C0M port converter. It seems there are two tricks to getting it to work so thought make quick note of what they are.

1. Add the following to the registary:

[HKEY_LOCAL_MACHINE\SOFTWARE\Tablet]
"PnPTablets"=",COM7"

2.Create a file called tablet.dat containing the below and place it in your system32 folder:

PrefsFileVersion 4
DriverOn1Off0 1
DriverLanguage 0
LeftHandedMouse 0
FunkyButtonMode 0
NoStartWarnings 0
HowManyTablets 1
TabletType 0
TabletModel 203
CommPort COM2
TabletOn1Off0 1
TabletPhysicallyOn 1
TabletFlags 0
HowManyTransducers 0

Hope this saves someone 3 hours 🙂

InitParams

Used InitParams for the first time today.

As a parameter of the Silverlight object in the HTML


In your C# code

if (e.InitParams.ContainsKey("videosource"))
{
  VideoSource = e.InitParams["videosource"];
}

Expression Media Encoder – The code behind

Just been learning some new things about the Expression Media Encoder templates as I go through and re-skin them for various projects. Need to jot it all down before I forget.

The templates use Control Templates so the code for a User Control exists in only one place. But this makes the structure of the code a little unusual. Elements within the control are brought into the script by a class called GetTemplateChild

m_toolsButton = GetTemplateChild(ToolsButton) as ToggleButton;

Without this you can’t access elements in the Control Template. Thankfully most of the elements have been exposed already. However I couldn’t pick out at what point these elements get exposed.

For the media element you can add a PlaylistItem at any point so you don’t need to worry about accessing the media element’s source.

ExpressionMediaPlayer.PlaylistItem item = new ExpressionMediaPlayer.PlaylistItem()
{
     MediaSource = new Uri(VideoSource),
     Description = "blah",
     IsAdaptiveStreaming=false
};
myPlayer.Playlist.Items.Add(item);