Drag and drop Silverlight example
This is a quick and simple example of how to create a draggable object in Silverlight 2.
- Create a Rectangle and set the RenderTransform to a TranslateTransform.
- Register the MouseLeftButtonDown, MouseLeftButtonUp, and the MoseMove events for the rectangle.
- In the MouseLeftButtonDown and MouseLeftButtonUp event you will capture and release the capture of the mouse, respectively. Also, you will need to store if the mouse is captured.
- Finally, update the position if the mouse is captured and the mouse is moving.
<Rectangle x:Name="rect" Width="50" Height="50" Fill="Blue" HorizontalAlignment="Left" VerticalAlignment="Top">
<Rectangle.RenderTransform>
<TranslateTransform x:Name=”rectTranslateTransform” X=”0″ Y=”0″ />
</Rectangle.RenderTransform>
</Rectangle>
this.rect.MouseLeftButtonDown += new MouseButtonEventHandler(rect_MouseLeftButtonDown);
this.rect.MouseLeftButtonUp += new MouseButtonEventHandler(rect_MouseLeftButtonUp);
this.rect.MouseMove += new MouseEventHandler(rect_MouseMove);
// Stores if the mouse is captured private Boolean isRectMouseCapture = false; void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this.rect.ReleaseMouseCapture(); isRectMouseCapture = false; } void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.rect.CaptureMouse(); isRectMouseCapture = true; }
void rect_MouseMove(object sender, MouseEventArgs e) { if (isRectMouseCapture) { this.rectTranslateTransform.X = e.GetPosition(this).X; this.rectTranslateTransform.Y = e.GetPosition(this).Y; } }
Final product:
Maintain mouse position on object while dragging
The above example snaps the pointer to the top left of the object. Face it, it’s annoying, what you really want is the mouse to maintain the position on the element while dragging. To solve this problem is simple.
- Add a variable to save the position when the user clicks the object.
- Save the position of where the mouse is located on the MouseLeftButtonDown event. The updated MouseLeftButtonDown event looks like:
- Finally, in MouseMove event subtract the clickPosition from the mouse position.
private Point clickPosition;
void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { clickPosition = e.GetPosition(sender as UIElement); this.rect.CaptureMouse(); isRectMouseCapture = true; }
this.rectTranslateTransform.X = e.GetPosition(this).X - clickPosition.X; this.rectTranslateTransform.Y = e.GetPosition(this).Y - clickPosition.Y;
Update
Drag and drop with physics (friction)
After playing around, I have updated this example to incorporate physics. Take a look:
http://www.coreyschuman.com/Silverlight/Prototypes/DragDropPhysics/Proto1TestPage.aspx
8 days left to the Olympics
After months and months the Olympics are almost here. This will be a big day/week(s) for internet broadcasting and for Silverlight. I can’t wait!!!
It looks like Microsoft has a page promoting the site http://www.microsoft.com/silverlight/NBCOlympicsonMSN/getsilverlight.html
Silverlight and the NBA
Today when I was looking Silverlight.net I noticed the headlining application was the NBA Website. I HAD to click it. All in all it was disappointing. Granted, this is probably a preview application of things to come for the upcoming season, still, I was expecting more.
A few thoughts:
1.) Why fullscreen? It is a mistake to have features the user can only use in fullscreen. This is a big problem. The high-res images are great, but make the browser window resize instead of the SL piece.
2.) Use of the Hand cursor. Clickable objects on the screen should have a visual indicating clickablity. Just having an object on the screen is not enough to let the user know action can be performed on it.
3.) The animations don’t feel thought out. Specifically the bottom right animation. Why is the bottom menu animating opacity and the transform? It is nice to see some easing on the menus…they need to be tighter.
4.) Video. Where is the video? I know I know, this is probably a prototype to generate interest, but how cool would that be to have Video in this app.
General Silverlight 2 concerns
As I continue working on Silverlight projects, a concern keeps creeping up…What happens after the first wave of really cool Silverlight applications? A number of applications being released are done with large teams and are rooted in usability and have design teams working in Blend. My biggest fear is after the RTM release we are going to see a year or two of really ugly Silverlight 2 applications designed by developers.
Microsoft has always had an army of .NET developer. With this new offering (Silverlight 2) developers are going to be chopping at the bit to get season in those skills.
So here it is…to all developers interested in Silverlight: Think of usability first. Design your application with the user in mind. Take time to talk to a user.
That’s all I have today.
Favorite Visual Studio 2008 keyboard shortcuts
Here are a few indispensable keyboard shortcuts I use on a daily basis:
- Ctrl+K,Ctrl+C - Comment selected block of code
- Ctrl+K,Ctrl+U - UnComment selected block of code
- Ctrl+K,Ctrl+F - Format selected block of code
- Ctrl+Shift+B -Build solution
After doing some searching for a full list of keyboard shortcuts in VS2008, I found the below PDF.
http://www.microsoft.com/downloads/details.aspx?familyid=e5f902a8-5bb5-4cc6-907e-472809749973&displaylang=en
Enjoy.
Div on top of Silverlight
Have you tried overlaying a <div> on top of a Silverlight object? The only way to achieve this is by setting the Windowless property on the Silverlight object to true. Otherwise you will get get a result like figure 2.
Figure 1 – Windowless set to true.
Figure 2 - Windowless not set or set to false.

NBC Olympics video player
Over the weekend two big events happened in the world of Silverlight:
- Silverlight 2 Beta 2 released,
- NBCOlympics.com Silverlight 2 video player released.
We, Schematic, along with NBC, Microsoft, and DeltaTre have been working hard to get this to production for a live event. Currently on the site you can watch short clips of sports and interviews. When the Olympics trials and the Olympics arrive, you will be able to watch live sports with this video player.
Taking a step back, it’s amazing to think the majority of live Olympics sports will be broadcast in the US. Every other Olympics, that I remember, have only broadcast the sports that sell well (Beach volleyball, basketball, swimming). Sports like table tennis, shooting, weight lifting, or judo fan always take a back seat. This is a real opportunity for those sports to shine.
Links:
http://www.nbcolympics.com/video/
My boy Rafael’s post about the player
His second post
Popup Player
Enhanced player
Olympic sports module
WordPress blogging software
Lately I have been very busy with work and haven’t had much time to blog. However, the project is winding and it’s time to hop back on the blogging train. On an semi-related note, I have been using twhirl, an Adobe Air app for Twitter, so I decided to search for “Adobe Air WordPress”. Airpress came up, unfortunately their site is down, making it difficult to test. Then the bright idea came up try “WordPress Microsoft Word” and sure enough it’s built in. This site explains how to publish with Word 2007.
Screen shot of AirPress. This is mainly to test uploading images through the publish utility in Word.
Animations in Blend
The addition of the animation engine in Windows Presentation Foundation (WPF) allows designers and developers to animate objects without using third party software. Before getting into an example of how to work with animations, it is important to know the basics.
- Animations are time based. After setting a target value the animation interpolates the values from the current value to the target value.
- Storyboards are the container holding animations. Each Storyboard can hold multiple animations.
- Animation types include colors, numeric properties, points, and strings.
A simple example of a ball bouncing will show how to create animations in Blend.
Tip
Press the Shift key while drawing your ellipse. This ensures a perfect circle.
Create the Storyboard
First things first, create a ball. Use the ellipse tool from the toolbox and draw a circle on the Artboard. The next step is to create a Storyboard to hold the ball dropping animation. On the Interactions panel click the plus sign, Figure 1. A box will pop up asking what you would like to name your animation. Everyone has their own naming conventions. For simplicity, this example will stick with the default Storyboard1 name.
After creating the Storyboard you will notice the interface has changed. The Interactions panel expanded, a timeline appeared, and a red border lines the Artboard. This lets you know it is time to animate.
Animation workspaces
Blend accommodates for two different views when designing animations, the Design workspace and the Animation workspace. To switch between views click Window -> Activate Workspace -> Design Workspace/Animation Workspace. Figure 2 and Figure 3 show the difference between the two views.
Figure 3 - Animation workspace

Keyboard shortcut
F6 toggles between the Design Workspace and the Animation Workspace
Add animation to the Stoyboard
Now the Storyboard has been created, add the ball dropping animation. Click on the first second in the timeline, and then move the ball to the bottom of the screen. Notice a white dot appears on the timeline across from the ellipse. This indicates a keyframe has been created for that element at that time. Click on the play icon above the timeline to test the animation.
What values are changing - Transforms
The value that was changed when you moved the ball down was the Y value of the TranslateTransform. Transforms are used for positioning, sizing, and rotating. There are four types of transforms: Translate, Scale, Skew, and Rotate. When moving or sizing in the Artboard, Blend changes the value of the transforms by default. It is possible to animate the Height and Width properties, but you will have to do this by using the property window instead of changing the size of the object on the Artboard.
Every UI element has a Transform. You can find the Transform palette in the properties window. There are six tabs Translate, Rotate, Scale, Skew, Center Point, and Flip in that order.
Make the bounce look natural
Up to this point you only have a ball that falls. There are three actions left to make the ball bounce look more natural: add easing, change the shape of the ball once it hits the ground, and make the ball bounce back up.
Easing
Currently the ball is falling at the same rate, making the animation feel unnatural. In general to make animations more natural you need to add acceleration and deceleration. In Blend this is done by Easing. Click on the white dot on the timeline. The Easing palette appears in the properties window. Click and drag the bottom left dot until you get a curve similar to that in Figure 6. With this curve, the ball will slowly drop then pick up speed as it approaches the ground, simulating gravity.
The ball hits the ground
Think of a racquetball hitting the ground. Once on the ground it changes shape, even if only for a split second. Along with simulating gravity this makes the bouncing ball animation more natural. Just like creating the first animation, click on timeline around 1.2 seconds. Then on the Artboard make the height of the ball smaller. Press play.
Figure 7 - Ball hitting the ground

Finally, make it bounce
Storyboards have the ability to automatically reverse themselves along with repeating the animation. To make the ball bounce you will make the Storyboard reverse and have the behavior repeat. On the Interaction panel click the title of the Storyboard, Figure 8. In the properties window the Common Properties for the Storyboard appear, Figure 9. Check AutoReverse and set the RepeatBehavir to Forever. Press Play
Figure 8

Create a video player in Silverlight 2 - Part 2
In part 1 we covered setting up a very basic video player. In part 2 we will add
- Scrubbing the video
- wiring up volume,
- and muting.
In future versions I will show how to display a buffer message, skin the controls, and how to work with media that’s not embedded in the xap.
- Scrubbing
- Add a MouseLeftButtonDown and MouseLeftButtonUp events for the slider in the constructor.

- Add a property to lock the scrubber while the mouse button is down.

- In the MouseLeftButtonDown event, set the scrubber lock to true.

- In the MouseLeftButtonUp event, release the lock on the scrubber and update the position of the video.

- Finally in the Tick event of the timer, prevent updating the position of the slider while the scrubber is locked.

- Add a MouseLeftButtonDown and MouseLeftButtonUp events for the slider in the constructor.
- Volume
- XAML. It important to set the Min to 0 and the Max to 1. The MediaElements volume ranges from 0 to 1.

- Register the ValueChanged event in the constructor for the slider.

- Set the video’s volume to the value in the ValueChanged event.

- Set the initial position of the slider to the video’s volume. You can either do this in the CurrentStateChanged event or the MediaOpened event of the MediaElement.

- XAML. It important to set the Min to 0 and the Max to 1. The MediaElements volume ranges from 0 to 1.
- Mute button
- Resulting player

Files:
Instead of the whole project (because the video is embedded in the project), here are the two important pieces of this project
Comments(6)










