A new home

I’ve made the move to WordPress!

You should be automatically redirected in 5 seconds. Please visit World Wide Code on Wordpress

Hope to see you there!

Sunday, February 27, 2011

Panorama versus Pivot: Windows Phone 7


The most popular controls in the Windows Phone 7 tools are the Panorama and pivot. Their usage is somewhat similar, but both provide a different kind of experience.
Here's a link to a post from Tim Heuer's blog, Pano versus Pivot: Windows Phone Design Days content available.
The post provides good insights on the two controls and has links to some good learning videos on windows phone 7.
If you are interested in a much more detail look into the implementation of these two controls consider visiting Windows Phone 7 - Part #5: Panorama and Pivot controls

Saturday, February 26, 2011

Accessing data grid row from an event


When using a Data grid in Silverlight, there comes a situation when you want to get the row(or cell) that contains your text box, hyperlink button or any other control. You try to see if the Data Grid has a property like Rows or something which can give you the handle of the current row. Many people do not know is that the DataGridRow class has a static method GetRowContainingElement which takes the control that you have placed in that row and returns you that row object.
So, consider you have a button in some column of a row, now add an event handler for the click event of the button. The following code describes the way to get the clicked row and its related objects.
private void Button_Click(object sender, RoutedEventArgs e)
{

//Get clicked row
var dataGridrow = DataGridRow.GetRowContainingElement(sender as FrameworkElement);
//Or get clicked index
var roIndex = DataGridRow.GetRowContainingElement(sender as FrameworkElement).GetIndex();
//Or if you need any column of clicked row
//Suppose column1 contains a TextBox
var textBox = myDataGrid.Columns[1].GetCellContent(dataGridrow) as TextBox;
}
It is not necessary to use the GetRowContainingElement method in a click event any event that you have subscribed will do the task.

Using Icons in Windows Phone 7 App



When using icons, you just think of a general colorful image that you have just found on Google. It might not be easy as you think, when you want to include icon in the application bar of your windows phone 7 app. Being from Microsoft, Windows phone 7 puts its own restrictions on using icons, and no they are not limited to just size.
Heres what msdn article Application Bar Best Practices for Windows Phone puts it
The following items apply to the Icon Buttons displayed by the Application Bar
  • Icon images should use a white foreground on a transparent background using an alpha channel. The Application Bar will colorize the icon according to the current style settings and colored icons can cause this effect to display unpredictably.
  • The circle displayed on each Icon Button is drawn by the Application Bar and should not be included in the source image.
  • Icon images should be 48 x 48 pixels in size. The white foreground graphic for the button should fit in a 26 x 26 area square in the center of the image so that it does not overlap the circle.
  • Do not use an Icon Button for a Back button that navigates backwards in the page stack. All Windows Phones are required to have a dedicated hardware Back button that should always be used for backward navigation.
  • Use Icon Buttons for the primary, most-used actions in your menu. Some actions are difficult to convey clearly with an icon. If this is the case, consider using a Menu Item instead.
  • Choose icons that have clear meanings when the Application Bar is rotated. The Application Bar automatically handles changes in screen orientation. When the device is in a landscape orientation, the menu is displayed vertically on the side of the screen. The icon buttons are rotated so that they appear upright to the user, but the order of the icons in the list is not changed. It is possible for icon meanings to be confused when this occurs, particularly if two of the icons are mirror images of each other along the Y axis.
In short, you need to use icons which have the icon shape in white with a transparent background and having size of 48 x 48 pixels.
By installing Windows Phone Developer Tools, various icons are installed with it. You can find them at one of the following locations:
  • C:\Program Files\Microsoft SDKs\Windows Phone\v7.0\Icons\dark
  • C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Icons\dark
Here are some icons from that pack:
appbar.feature.camera.rest appbar.feature.email.rest appbar.feature.search.rest appbar.feature.settings.rest appbar.feature.video.rest appbar.folder.rest appbar.minus.rest appbar.new.rest appbar.next.rest appbar.questionmark.rest appbar.refresh.rest appbar.save.rest appbar.share.rest appbar.stop.rest appbar.sync.rest appbar.transport.ff.rest appbar.transport.pause.rest appbar.transport.play.rest appbar.transport.rew.rest appbar.upload.rest appbar.add.rest appbar.back.rest appbar.basecircle.rest appbar.cancel.rest appbar.check.rest appbar.close.rest appbar.delete.rest appbar.download.rest appbar.edit.rest appbar.favs.addto.rest appbar.favs.rest
Related Links

Tuesday, February 8, 2011

Creating a collapsible panel using storyboard

Every time when you want to create a pane that can collapse or expand, you have to write either a Storyboard in xaml or some other animation logic. This can be a tedious task and has many pitfalls. Some of them being
  1. Change in width of the pane requires rewiring of the storyboard.
  2. Consistency in the collapse/expand duration of the pane.
  3. Flexibility in changing the pane from vertical to horizontal.
To solve all of these and many more problems that may arise we need to build a custom PaneManager that will do this for us when required.
At first we must create a class in your Silverlight application, lets call it PaneManager, with the following members
public class PaneManager
{
#region Public Members
public TimeSpan Interval { get; set; }
public double Increment { get; set; }
public double MinValue { get; set; }
public double MaxValue { get; set; }
public Grid Grid { get; set; }
public int Index { get; set; }
public GridUnitType GridUnitType { get; set; }
public Orientation Alignment { get; set; }
#endregion
}
Let us go over each member in detail.
  • Interval - This defines the duration to wait between successive calls to make the pane transition.
  • Increment - This is the value by which the pane's width or height must change.
  • MinValue - This defines the minimum threshold of the pane's width or height.
  • MaxValue- This defines the maximum threshold of the pane's width or height.
  • Grid - This is the parent Grid that contains the desired pane.
  • Index - This is the index of the row or column containing the pane.
  • GridUnitType - this is the unit of measurement of the Grid's row/column.
  • Alignment - This specifies whether the pane transition is to be done on a row or column.
We will also need to add the following members and events to the PaneManager class
#region Private Members
private Storyboard _storyboard = new Storyboard();
#endregion
#region Public Event
public event EventHandler Completed
{
add { _storyboard.Completed += value; }
remove { _storyboard.Completed -= value; }
}
#endregion
We have until now created the basic structure of the PaneManager without adding any functionality as such. To make this class usable,

Tuesday, February 1, 2011

DispatcherTimer vs Storyboard


Many of you must have heard and used the Dispatcher Timer class in Silverlight for all sorts of things. One common usage is for animation purpose, which is an incorrect practice.
Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. The Dispatcher Timer is also influenced by the UI Thread, so if any heavy rendering is to be done can cause you animation to be rigid and many a times slow.
Silverlight also has a class called Storyboard which can solve these issues and is a better solution than the Dispatcher Timer. The Storyboard has simple properties like TargetProperty, TartgetName which specify which property of which element you would like to animate. The Storyboard supports several animations namely, Color Animation, Double Animation, Point Animation andObject Animation Using Key Frames
Using a Storyboard in XAML


<StackPanel>
  <StackPanel.Resources>
    <!-- Animates the rectangle's opacity. -->
    <Storyboard x:Name="myStoryboard">
      <DoubleAnimation
        Storyboard.TargetName="MyAnimatedRectangle"
        Storyboard.TargetProperty="Opacity"
        From="1.0" To="0.0" Duration="0:0:1"
        AutoReverse="True" RepeatBehavior="Forever" />
    </Storyboard>
  </StackPanel.Resources>

  <Rectangle
    x:Name="MyAnimatedRectangle"
    Width="100" Height="100" Fill="Blue" />
</StackPanel>
Heres a link to create an Animation in Procedural Code if you need.


In Conclusion
I would suggest avoid using the Dispatcher Timer whenever possible. If you have any queries, you can get back to me.


Related Links