Archive

Archive for January, 2012

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: http://www.amazedsaint.com/2009/12/silverlight-listening-to-dependency.html

Screen Capturer – use Camera key to get screenshot

January 23, 2012 Leave a comment

If you are looking for an application on Windows Phone 7 that will capture your screen and save into an image file, then you can download a tool (ScreenCapturer-20111023v3.xap) here:
http://forum.xda-developers.com/showthread.php?t=1316199

You need a ‘developer unlocked’ phone, then side load the screen capture app.

User Guide:
= Steps =

1. Click “Start Capture Task” to start capturing (A timer will start, and during this period, application will listen to Camera button event)
2. You may need to wait one or several seconds, a message box will show up to let you know you can start capturing.
3. Navigate to any application, and touch Camera button to capture the screen
4. A message box will show if the capturing is success, you can capture 20 images a one time (yes, you can ask for more if you want)
5. When the Capture Task ends, a Toast will show up to let you save the captured image, tap the Toast to return to application
6. The thumbnail will list and the captured images will stored in Pictures Hub
7. Tap the thumbnail will delete the temporary image file in the application folder

= Settings Help =

* Prevent Capture Task auto run: Capture Task uses BackgroundAgent to run and wait for Camera button event, BackgroundAgent may auto run in some case, enable this option to stop auto capture.
* The Capture Task duration: How long will the Capture Task run before it stop to accept Camera button event.

= Notice =

* Game and movie is not supported in this version.
* The Capture Task only save temporary file, you need go back to application to trigger saving file to Pictures Hub.

Use it for your own risk.
Hope it helps!

Categories: Windows Phone 7.5, WP7

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=”http://schemas.microsoft.com/windowsphone/2009/deployment&#8221; AppPlatformVersion=”7.1″>
<App xmlns=”” BitsPerPixel=”32″

That’s it!

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

Reasons:
– 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).

Source: http://www.istartedsomething.com/20111113/death-to-banding-windows-phone-devs-please-upgrade-your-apps-to-32-bitsperpixel/