How to change the startup page on a WP7 application

Sometimes you may need to change the startup page of Windows Phone application. By default it will be pointing to MainPage.xaml. You can modify the WMAppManifest.xml to change the default page. In the WMAppManifest.xml you will find the following.(You can get the WMAppManifest.xml by expanding the properties node in the Project Explorer window.)

<Tasks>
  <DefaultTask  Name ="_default" NavigationPage="MainPage.xaml"/>
</Tasks>

Happy Programming :)

How to convert JSON to XML and vice versa in C#

Json.NET supports converting JSON to XML and vice versa using the XmlNodeConverter. The JsonConvert has two helper methods for converting between JSON and XML. The first is SerializeXmlNode(). This method takes an XmlNode and serializes it to JSON text.

string xml = @"<catalog><book id=""bk101"">" +
    "<author>Gambardella, Matthew</author>" +
    "<title>XML Developer's Guide</title>" +
    "<genre>Computer</genre>" +
    "<price>44.95</price>" +
    "<publish_date>2000-10-01</publish_date>" +
    "<description>An in-depth look at " +
                "creating applications with XML.</description>" +
"</book>" +
"</catalog>";

var doc = new XmlDocument();
doc.LoadXml(xml);

var jsonText = JsonConvert.SerializeXmlNode(doc);
Console.WriteLine(jsonText);

The second helper method on JsonConvert is DeserializeXmlNode(). This method takes JSON text and deserializes it into a XmlNode.

var xmlNode = JsonConvert.DeserializeXmlNode(jsonText).OuterXml;
Console.WriteLine(xmlNode);

Happy Programming :)

How to prevent automatic screen lock in WP7

If you are developing some travel applications or fitness applications, you may want to disable automatic screen locking feature of Windows Phone. You can disable this using UserIdleDetectionMode property of PhoneApplicationService.Current class.

Microsoft.Phone.Shell.PhoneApplicationService.Current.UserIdleDetectionMode
    = Microsoft.Phone.Shell.IdleDetectionMode.Disabled;

You can find more details about Idle Detection for Windows Phone and best practices here

How to detect if the application is in design mode in WP7

Sometimes you may require to detect whether the Application is in Design mode or not in the view model, otherwise if you are creating instance of DataContext or using Application object etc, XAML designer will not display the UI. You can detect the design mode in WP7 using DesignerProperties.IsInDesignTool property, if it is true, you are in designer.

Here is the implementation.

get
{
    var taskRepository = new TaskRepository();
    if (!DesignerProperties.IsInDesignTool)
    {
        //In runtime, return the values from the database
        _tasks = taskRepository.GetAllTasks();
    }
    else
    {
        //In designer, return the default values
        _tasks = taskRepository.GetDefaultTasks();
    }
    return _tasks;
}

You can find more details about DesignerProperties.IsInDesignTool in MSDN

Happy Programming :)