Archive for the ‘C#’ Category

Listening to a Dependency Property’s Change notification

January 31, 2012 Leave a comment

I was digging a bit around Silverlight Dependency properties, mainly to see how to receive change notification when ever a dependency property is changed. In WPF, this is straight forward, you may use the DependencyPropertyDescriptor, and call AddValueChanged. Like this.

  1. DependencyPropertyDescriptor desc =
  2.     DependencyPropertyDescriptor.FromProperty
  3.     (UIElement.VisibilityProperty, typeof(UIElement));
  4. desc.AddValueChanged(this.myLabel, new EventHandler(VisibilityChanged));

Now, how to do this in Silverlight? I roamed around a bit, but can’t really find a good way of doing that. So, here is a quick hack. The trick is to use DependencyProperty.RegisterAttached(..) to instantiate a dependency property type (Ahem). This is what we are doing below.

  • Create a binding with the FrameworkElement as the source
  • Use DependencyProperty.RegisterAttached(..) to create a DependencyProperty instance
  • Use FrameworkElement.SetBinding(dp, binding) to associate the dependency property with the element, via the binding.

So, we’ll get a call back when ever the dependency property changes. The RegisterForNotification summarizes what I explained above.

  1. /// Listen for change of the dependency property  
  2. public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
  3. {
  4.     //Bind to a depedency property  
  5.     Binding b = new Binding(propertyName) { Source = element };
  6.     var prop = System.Windows.DependencyProperty.RegisterAttached(
  7.         “ListenAttached”+propertyName,
  8.         typeof(object),
  9.         typeof(UserControl),
  10.         new System.Windows.PropertyMetadata(callback));
  11.     element.SetBinding(prop, b);
  12. }

Create a new Silverlight project, and add the above code to the code behind. And add the following controls to your xaml page.

  1. <Grid x:Name=“LayoutRoot” Background=“White”>
  2.      <TextBox Text=“hello” x:Name=“txtMain” />
  3.      <Slider Value=“10” Minimum=“0” Maximum=“100” x:Name=“sliderMain”/>
  4.  </Grid>

Now, just call RegisterForNotification from where ever you need (I’ve it in the Constructor of my MainPage.cs, just under the InitializeComponent() call), like

  1. //Shows a message box when the text of Textbox or value of Slider changes.  
  2. RegisterForNotification
  3.     (“Text”, this.txtMain, (d, e) => MessageBox.Show(“Text changed”));
  4. RegisterForNotification
  5.     (“Value”, this.sliderMain, (d, e) => MessageBox.Show(“Value changed”));

Fire up your project, and you’ll see the message boxes when ever the value change happens. So, now it looks pretty interesting, I guess. Opens up possibilities like having Data triggers in Silverlight, probably via an attached behaviour. Need to evaluate the pros and cons of this approach, came across this scenario while doing some other hacks.

Original blog post by Anoop Madhusudanan:


Death to banding: Windows Phone devs, please upgrade your apps to 32 BitsPerPixel

January 12, 2012 1 comment

I’m sure that you’ve noticed this banding effect in your application as soon as you used gradient brush or image resources.

So here is a less known feature that Windows Phone 7.5 “Mango” introduced to fix color bandings.

The magic attribute is BitsPerPixel="32" inside the App tag of WMAppManifest.xml.

 <?xml version=”1.0″ encoding=”utf-8″?>

<Deployment xmlns=”; AppPlatformVersion=”7.1″>
<App xmlns=”” BitsPerPixel=”32″

That’s it!

You may ask why runs  WP7  in 16-bit color?

– Battery life
– Performance (half as much data to move around)
– Memory (back buffers are half the size)
– Widest compatibility (not all displays used by manufacturers can handle 24/32-bit)
– Very few apps need more than 16-bit

So Windows Phone defaults to 16-bit, and apps can opt-in to 32-bit (but they may not always get it due to hardware limits).


Free e-book: Building Windows Phone Apps: A Developer’s Guide

September 30, 2011 Leave a comment

This book combines useful, technical information about the platform, written by those who’ve been there and done it, with some stories about the people and the apps they’d written. The book will act as a handy reference that gives you a “puddy up” into developing on the platform and, importantly, helps you to avoid making the same mistakes others have made.


Categories: C#, Ebook, Silverlight, WP7

Animated Splash Screen in WP7

When you create a new Windows Phone 7 project Visual Studio adds a SplashScreenImage.jpg image into it. This image is displayed until your application is loaded. The problem is that the SplashScreenImage.jpg is just a static image. So the question is how can we add an animated ProgressBar to splash screen.

Alex Yakhnin has a great solution on his blog:

The source code:

Categories: C#, Silverlight, WP7

Top 10 Books for Windows Phone 7 App and Game Development

Categories: C#, Silverlight, WP7