Getting Deep into .net

April 21, 2012

Windows Phone Tasks in Caliburn Micro Way

Filed under: Caliburn Micro,How To,Windows Phone 7 — goldytech @ 2:54 pm
Tags: ,

I had blogged earlier about my windows phone 7 app ,Bollywood ringtones which is now reaching almost 10k downloads. In that post I promised my readers to share my technical findings for my this pet project. I am man of my commitment and here I am to present my first knowledge share to the community. This post shows you how you can use the Caliburn Micro Framework for handling the Windows Phone Tasks. I specifically talk about SaveRingtoneTask but the same funda can be applied to most of the tasks (Launchers and Choosers) like EmailComposeTask,PhoneNumberChooserTask etc.

This blog post assumes that you are already aware of MVVM pattern and has some basic knowledge of Caliburn Micro Framework as this topic deals with some advanced concepts of this framework. I strongly recommend to read the documentation.

Caliburn Micro has much neater way of handling the launchers and choosers in MVVM pattern. The entire heavy lifting is done by IEventAggregator interface which is injected in the constructor of the ViewModel. Let us see the code to make things more clear.

Setting up windows Phone Ringtone in Caliburn Micro
  1. using Caliburn.Micro;
  2. using Microsoft.Phone.Tasks;
  3. using System.Windows;
  4. namespace RingtoneSample {
  5.     public class MainPageViewModel : Screen, IHandle<TaskCompleted<TaskEventArgs>>
  6.     {
  7.         readonly IEventAggregator _eventAggregator;
  8.  
  9.         public MainPageViewModel(IEventAggregator eventAggregator)
  10.         {
  11.             _eventAggregator = eventAggregator;
  12.         }
  13.  
  14.  
  15.        
  16.  
  17.         /// <summary>
  18.         /// The Button Handler
  19.         /// </summary>
  20.         public void SetRingtone()
  21.         {
  22.             _eventAggregator.RequestTask<SaveRingtoneTask>(r=>{
  23.                 r.DisplayName="My Ringtone";
  24.                 r.IsShareable = true;
  25.                 r.Source = new System.Uri(@"appdata:/MyRingtone.wma", System.UriKind.RelativeOrAbsolute);
  26.              });
  27.         }
  28.  
  29.         /// <summary>
  30.         /// IHandle Interface Implementation
  31.         /// </summary>
  32.         /// <param name="message"></param>
  33.         public void Handle(TaskCompleted<TaskEventArgs> message)
  34.         {
  35.             switch (message.Result.TaskResult)
  36.             {
  37.                 case TaskResult.OK:
  38.                     MessageBox.Show("Ringtone is set");
  39.                     break;
  40.                 case TaskResult.Cancel:
  41.                     MessageBox.Show("Save was cancelled");
  42.                     break;
  43.                 case TaskResult.None:
  44.                     MessageBox.Show("No information");
  45.                     break;
  46.             }
  47.         }
  48.  
  49.         protected override void OnActivate()
  50.         {
  51.             _eventAggregator.Subscribe(this); //Register for event
  52.             base.OnActivate();
  53.         }
  54.  
  55.         protected override void OnDeactivate(bool close)
  56.         {
  57.             _eventAggregator.Unsubscribe(this); //Unregister
  58.             base.OnDeactivate(close);
  59.         }
  60.     }
  61. }

I would like to bring few things to your mind here from the above code. These are bare minimum piece of code that you must write to make this work.

  • Your ViewModel class must implement  IHandle interface of Caliburn Micro Framework. If the windows phone task class have the TaskCompleted Event then this interface should be something like, IHandle<TaskCompleted<EventCompletedEventArgs>>. The TaskCompleted belongs to Caliburn Micro whereas the EventCompletedEventArgs should be replaced by the appropriate task completed EventArgs .So in my code for the SaveRingtoneTask has the completed event attached to it my IHandle interface is of the TaskEventArgs. This interface has the Handle method which is synonymous to the EventHandler. You can use this method as your callback method once the event is triggered and it is completed.
  • The Viewmodel class constructor should be injected with IEventAggregator.
  • I am using IEventAggregator RequestTask<SaveRingtoneTask> method and passing all the properties of the SaveRingtoneTask class via lambda expressions. The RequestTask method is similar to the Show method of the task ,if you take the classical approach of handling the Tasks.
  • You need to call the subscribe method when your viewmodel loads as my viewmodel is also inheriting from the Screen class I am using the OnActivate override method to do this job. This is very important step otherwise you may not get the callback event handler.
  • And finally you need to unregister so that the memory clean up happens. The good thing over here is that IEventAggregator creates a WeakReference of the events hence there are no chance of memory leaks. But it is always good practice to unregister all the hook events. I am performing this task by overriding the OnDeactivate method.

 

So you have seen that using ViewModel first approach how innovatively we handled the launchers and choosers in windows phone 7 , keeping the essence of MVVM pattern. You can download the sample code for this post from here.

Cheers and keep rocking.

March 11, 2012

Bollywood ringtones is now available in Windows Phone Marketplace

Filed under: Windows Phone 7 — goldytech @ 4:09 pm

 

Today I received the email from Windows Phone Marketplace team that  my application, Bollywood ringtones is now published. Well this is my first windows phone 7 app in the market place. This blog post shares my experience right from the conceptual idea to the final publishing. I always intended to keep my blog technical but this post would be a bit exceptional but I assure you that I will share the secrets and some tricks which I discovered during this pet project of mine with the community in future posts. And later or sooner if time permits I will make this app an open source and will publish on codeplex.

THE FORCE BEHIND

You can call me fan boy of Microsoft. The company and its technologies have always inspired me and motivated me to such a extent that even after 12 years of industry experience I feel hard to get detached from the technology. I was early adopter of Windows Phone device as soon as hit the Indian shores. I was also a vivid user of Windows Mobile right from Win CE 5.0 to Win mobile 6.5 . It really amazed me the things I could do on this 4 inch device. So I used to check my emails and surf the net on my mobile device even eight years back Smile. Do you remember those HP-ipaq , Qtek PDA’s. One thing I must admit that those devices were extremely flexible and the SDK or .net Compact Framework had a very rich API’s and you could do wonders with them. Let me give you example of one small feature which was like a big push for me to develop this app. I could make any mp3 file as the ringtone by just long hold or right click on the selected file, a context menu would appear and all I need was to do select and option “Set as Ringtone”.  Windows Phone 7 is designed keeping consumer or end user in the mind rather than the developer (We developers want to do everything impossible Winking smile). So no wonder there wasn’t any such features in WP7 available which could go at OS level. There wasn’t even an api also available in pre mango SDK for setting up the custom ringtone. I hope the SDK will mature in up coming releases which will allow developer to flex their muscles.

I always missed it as I am great music lover and always keep on changing my ringtones every week to my favorite songs. There are other apps also in the marketplace which allows to set ringtones but not the Bollywood songs. Their lists are also pretty static and don’t keep updates. The USP of Bollywood ringtones is that I shall keep updating the ringtones every two weeks . If you as a user doesn’t find you favorite tune in the list just email me with its attachment and take my word it will be available in less than 48 hours. Below are some cool screen shots of the application.

 

MainPage RingtoneDetails RingtoneDetails1

TECHNICAL STUFF

I know you must be itching by now enough of talking share some tech info with us. So here we go. Bollywood ringtones is my pet project. I used to work on weekends for this. After long week of hard work at my day job somehow I used to get energy to sit down again and do the coding for my dream project. The app uses  Caliburn Micro 1.3.1 framework to achieve the MVVM pattern. Boss this is amazing stuff developed by Rob Eisenberg and has really impressed me. The learning curve was very little as I was already aware of this design pattern and there is an extensive documentation and examples available on the codeplex. The community support is immense I got answers to my queries in less than 24 hours which was really good and I was able to keep the pace of my development. Some of features used from this Framework are Actions, Coroutines, Tasks, EventAggregator ,StorageHandler etc.

The server part was done using ASP.net Web Api. Again a cool technology from Microsoft makes development breeze. Though I am client side developer it didn’t appear a rocket science to me and I got up and running in less than 1 hour of time. Thanks to Glenn Block for his guidance and help. I used Entity Framework 4.3 with code first approach and MVC 4 Beta (It comes with GO-LIVE license) for developing my api methods / controllers.

I also used the Telerik RAD controls for Windows Phone 7. This is not a breaking news that Telerik is leader in custom controls and their products speak for themselves what more I could say about them. Their examples project which comes as a part of download helped me lot to get started with the controls. I have a confession to be made over here that I am developer and not a designer so the entire artwork was done by my younger brother Nazim (@nazqu5). He was immense help to me for making the application look sexy and sweet. I provided him few guidance in Blend and gave him design time data for developing in Blend. All the images were developed in Photoshop.

I also wrote Unit Tests for every viewmodels in the project. I used Jeff Wilcox unit test framework to test them. It was very pleasing to eyes that every tests was green. Last but not least I used Marketplace Testkit for making my app marketplace ready. No wonder that my app got certified in the first attempt itself.

OTHER TOOLS

  1. SharpGIS GzipWebClient from Morten
  2. Json.net
  3. Metro Grid Helper
  4. Isolated storage Explorer

CONCLUSION

For me developing app for Windows Phone 7 platform was very easy as I already knew Silverlight and WPF. If I calculate the hours spent on developing the fully functional app took twelve hours of coding. This include writing MVC code also. I have not included the art work and unit testing in this. Those are separate. Though Bollywood ringtones is free it supports ads. I know that I wont become millionaire from this but at least I would be able to make enough money to meet my per month fuel expenses Smile. I would strongly emphasize that every developer must try their hands on it and grab this rising opportunity. Microsoft India has great contest for the developers which can won you Windows Phone device. So start rolling your app today.

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 70 other followers