About the author

Paul Maré is a Silverlight developer living in Sydney

Items of Interest

     
     
    Zero Gravity C#imp

    Some Side-Notes

    clock July 7, 2010 13:11 by author ZeroGravityChimp
    Hello readers,

    For a little bit of a change, I'm going to guide you through creating a simple application. The purpose of it is to play musical notes in response to clicks and keyboard commands. It is a digital piano.

    It's going to look like a piano. To achieve this, I've made it a fixed width application, and done the modifications to the UserControl below. If you need more detailed instructions on getting started see my earlier postings here.



    for the main notes (i.e. the larger white ones), we should create some columns. Create a grid inside the LayoutRoot in "Objects and Timeline" and add some columns. Start off by clicking them roughly along the top of the grid.



    we need 6 dividers to make 7 columns - one for each A, B, C, D, E, F, and G - the musical notes on a piano. Once these dividers are in place, you will see 7 "Star sized" icons above their respective columns. Click these icons to toggle until each is "Pixel sized".



    In the split view, or selecting each column by clicking beneath the icon, edit their width values and make them all equal.

    Create 7 Rectangles, by double clicking them on the toolbar on the left. This will add them with default settings. For each of these rectangles, reset the margins - since we will use the grids to determine the size of the rectangles, they can simply stretch.



    Name them starting from C to G, followed by A and B. That is how a piano octave is laid out. Now you need to change the Grid.Row property of each of these. The first should be 0, the next 1, and so forth up to 6. This can be edited in the "Layout" section of the properties of each Rectangle, or directly in the xaml.



    The next step is to add all of the audio files to the project. I have put the zip filled with the note sound bytes you will need here.

    I got these from an old open source Silverlight 1.0 Piano project which you can find here (also, the source, thanks to Chris Bowen's Blog). Credit to Microsoft for releasing this as part of their Silverlight 1 SDK back in the day. Our project won't replace, or be as good as, that one I just linked you to. Ours is just for learning and a bit of fun.

    Once you have downloaded them, add a new folder to the project called "Assets" and add the files to the folder using "Add existing item" from expression blend or Visual Studio as shown below.



    Rebuild the project now by pressing F5 to make sure it still runs without errors and the new files are recognised by the compiler.

    Now I'm going to introduce you to a feature of Silverlight called MediaElement. To find it, click the "More" icon along the bottom of the main toolbar and type in "media" in the search box. This will bring the control up into view. Note that other controls can also be found and browsed in this way.



    Go ahead and add 7 MediaElements to the page (you will need more eventually for all the sharp and flat notes in between) and name them the same as the notes but starting with Media_ like Media_C, Media_D, etc.

    These elements are designed to present some kind of media, in this case it is an audio file of wma format. It has functions like Play() and Stop() as well as a Position (like 00:00:01) which behaves like a track slider in common Media Players. We will use some of this functionality now.

    Select the MediaElement as you would any object in Objects and Timeline, and have a look at the Properties for that. You should be able to choose any of your wma files in the project as a "source". If you don't see them, you may need to rebuild or run the project.



    Choose the corresponding source for each element. You may also note that in the screenshot above i have grouped the keys with their media elements into a grid. This won't change much but it will make the project easier to read and understand. To group into grid, select some elements and press Ctrl+G.

    We want the Media to play based on what piano "Keys" are pressed. To do this, we need to act upon the click of each of the Rectangles we created earlier. Select the elements starting from C and click the "Events" tab as shown. Now all you need to do is double-click in the box next to "MouseLeftButtonDown" to add the event handler.



    You will then be automatically taken to the code file, and should see the following created for you:

    private void C_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
         // TODO: Add event handler implementation here.
    }

    The code we put in here will run when the rectangle is clicked. Change it as follows and run the project.

    private void C_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
         Media_C.Position = TimeSpan.FromMilliSeconds(0);
         Media_C.Play();
    }

    You will be able to hear a note play whenever you click on that rectangle.

    For the first round of this project I have implemented an event handler like that for each of the white squares, and played some notes using the mouse for fun. If you are following you would see that the piano doesn't really work very well yet, since you can't press multiple keys at once, and if you hold them in you get a terrible sound effect.

    In the next post I will add some keyboard events and change the code so that we have reusable functions instead of many separate event handlers. I'll also add the black (sharp and flat) notes into my project.

    Stay tuned!

    Zero Gravity Chimp


    Basics of Grid Layout: Setting the Design size

    clock March 19, 2010 04:40 by author ZeroGravityChimp

    Hello!


    This is the first in a long series of how designers can use Blend 3.0.

    I'm opening up a new project in Expression Blend 3.0 called GridLayoutBasics. If you don't know how, follow my earlier post here: http://www.zerogravitychimp.com/post/Custom-Scrollbars-1-Create-in-Expression-Blend.aspx
    The first thing you should know is that "LayoutRoot" which blend will automatically add to "MainPage.xaml" is a grid. Grids are designed to fill the space that they occupy, and their contents can resize automatically too.

    I'm going to go ahead and add some rectangles to the LayoutRoot in different colours. Now I'm going simply going to press "F5" and see the results.

    Now in my case I see the rectangles taking up a 640x480 space, because that is the size of grid i drew them on. So how do i get the grid to take up all available space?
    Fill the grid with a colour first, so you can see the actual size of the LayoutRoot. Next click the [UserControl] in "Objects and Timeline" and find it's height and width properties in the "Properties" panel. You can now click on the little "Auto" buttons that make your life a bit easier. Do this for width and height.

    Run the project again to see what has happened. Your LayoutRoot is probably taking up all the available space in the window, but only some of your rectangles have stretched proportionately, while others have stayed tiny.

    Back to Expression Blend. Select the "[UserControl]" in "Objects and Timeline" and locate the bottom-right arrow on the screen. Use this to resize the viewing area to a size that works for you.
    This is called setting the Design time size of the UserControl, and will not affect the final product. You can view the Xaml (By right-clicking the UserControl and clicking "View Xaml") to see the following:

    If you can't see the arrow I've circled in red you need to make sure you selected "[UserControl]" in the left-hand side panel "Objects and Timeline".


    I like to close the Xaml split view to give myself more space. This can be done easily by using the buttons at the top-right of the design area:


    But how can we control how all of these items will be resized at runtime? We need to use columns and rows and their properties to be able to control this dynamic resize. I will be addressing this in my next post.

    Please leave comments if you have any questions or input.

    Have fun designing,

    Zero Gravity Chimp



    Custom Silverlight Scrollbars Part 2: Customising!

    clock February 1, 2010 03:02 by author ZeroGravityChimp

    Hello,

    for those of you who followed the previous posting, I'm reopening my "Scrollbars" project now. If you are just reading this, I've created a basic application using only Expression Blend 3.0 and Silverlight 3.0. On my MainPage.xaml I have added a Scrollviewer containing a stackpanel, and that contains a bunch of coloured rectangles.



    When you run this project (f5) you can scroll downwards to see more rectangles. Now we are going to change the style of the scrollbars.

    What we need is to open up the default scrollviewer that microsoft has created for us. The scrollbar is situated inside the ScrollViewer Template. This is the trick you need, remember it: right-click the ScrollViewer, click "Edit Template" and then click "Edit a Copy".



    Now you choose a name for your new template. I'm just going with the defaults this time, and pressing 'OK'.



    The screen has not changed much once I press OK but the area on the left labelled "Objects and Timeline" has some new objects. You can open up the [Grid] to see VerticalScrollBar and HorizontalScrollbar.

    Right click the VerticalScrollbar (You can do the horizontal one instead if that’s what your project needs) and click "Edit Template", then "Edit a Copy".



    Again, I'm just going to go for defaults, thus creating "ScrollBarStyle1" in "This document..." when I press 'OK'.

    In this view, I'm going to start by looking inside VerticalRoot, since, in my application, the vertical scrollbar is currently visible.

    The components that are listed under "VerticalRoot" are the ones which make up the default scrollbar for Silverlight that was supplied to us by Microsoft. Now let's destroy it >:D !! Don't worry, we're editing a copy and it isn’t possible to destroy anything.

    I'm going to demonstrate one way to style a scrollbar, but you will probably want to do some things differently. I apologise for my poor design sense, that’s what we developers are known for.

    I'm going to start by getting rid of the three nameless [Rectangles] that were originally there for some sort of visual effects.



    Next I'm going to edit the template of the VerticalSmallDecrease. In this case I'm using "Edit Current" instead of "Edit a Copy".

    In this template, I've deleted everything except the "Path" and added a rectangle underneath the Path with a Black fill.

    To do this, delete the items, select Rectangle from the vertical toolbar on the left, double click the rectangle in the toolbar and move it above the "[Path]" in "Objects and Timeline". Then select the Rectangle on the left and change its "Fill" property on the right in the "Properties" section.

    Remember that the list in "Objects and Timeline" determines display order, whatever's at the bottom will display in front (unless you specify using ZIndex).


    Now is probably a good time to save your work.

    I've changed the Rectangle's Stroke property and the Path's Fill property to this colour #FF7F2CFF which can be pasted into blend here:



    I also changed the Path's Width to 12 and Height to 8 using the Properties area after selecting [Path] on the left hand side.

    The last thing we need to do before we are done with this VerticalSmallDecrease is find the "States" up in the top left. If you can't find it, you can activate it in the main "Window" menu. I've opened mine and it looks like this:



    If you click on a state here, you will be editing only for that state, unless you choose “Base”. For example, if you click MouseOver and make some changes, those changes will only be visible when you mouse-over this part of the scrollbar. When you are editing a "State", such as MouseOver, a pop-down alert will appear at the top of the editing area saying something like “MouseOver state recording is on”. Be careful what you do when recording is on, changes you make are only for that state.

    In my project, I'd like the "Fill" property of the path and the "Stroke" property of the rectangle to change based on Normal, MouseOver, Pressed, or Disabled state. Normal should generally be the same as Base.

    In base, I change the path's Fill to Black (#FF000000). then in MouseOver I change the path's Fill to #FF7F2CFF. I want the colour to go lighter when the button is "Pressed", so in Pressed state I change the colour of the rectangle Stroke and the path’s Fill to #FF9C5CFF. Finally, I'm going to change the rectangle Stroke to black in the Disabled state.

    I recommend you click through all your states one by one and see that they change as you would like. If I run my project now I can clearly see the modified scrollbar in action!



    Now that you know how to edit the SmallDecrease, you can do the same for the SmallIncrease. All you need to do to get started is go “Up a level” and right click on the right part of the scrollbar and, of course, choose “Edit Template”. But how? like this:



    the small arrow shaped grey name tags at the top act like breadcrumbs and you can follow them backwards step by step. To edit the VerticalSmallIncrease you need to go back one level, which can be done by going to (the same level as) "VerticalSmallDecrease".

    Now you should see the same view, and the ability to “edit template” on the parts of the scrollbar: VerticalSmallIncrease, VerticalThumb, etc.

    The VerticalThumb is the square part that floats between the arrows and shows how far down you are. It can be edited in a similar way as the smallincrease and decrease. The VerticalThumb has one default behaviour I'd like to keep. In "Disabled" state the "ThumbVisual" (which contains everything) becomes hidden.

    Other than that I pretty much got rid of everything except the "Background" element and changed the colours of that to suit me. I've also added 2 rectangles with height=2 and some margins (left and right = 1). They will become visible only on MouseOver and Pressed states.

    The last thing you need to change is the "LargeIncrease" and "LargeDecrease". This is the part of the scrollbar between the thumb and the arrow that will cause a "page down" effect if you click on it. This one is easy to edit, I just picked a background colour of Black (#FF000000 not #00000000) and margin left and right= 1.

    As a final touch, I decided to change the "VerticalSmallDecrease" margin-bottom to 0 and the "VerticalSmallIncrease" margin-top to 0 to get rid of the white space between the thumb area and the buttons.

    Now when I run my project I see this:



    Sorry, it isn't pretty, but again I am a coder and not a designer! I hope you can do a better job customizing your scrollbars.

    For Horizontal Scrollbars you can either create a new “ScrollBarStyle” or you can edit this one in the “HorizontalRoot” section. You can apply 1 scrollbarstyle to both horizontal and vertical scrollbars alike since the style has a definition for each.

    To apply this style to any scrollbar once you are done, Right-click the horizontal scrollbar, click “Edit Template”, “Apply Resource”, and choose the “ScrollbarStyle1”.



    If you find anything unclear please comment!



    Have fun customising,

    Zero Gravity C#himp