Windows Phone ‘Hello World’ App Using MVVM Light

When building any application, it is always a good practice to follow tried and tested design-patterns which will help in separation of concerns, code-reuse, maintainability, testability etc.… Just like ASP.NET there is MVC, for XAML based technologies (like Silverlight, Windows Presentation Foundation, Windows Store and Windows Phone apps), there is MVVM.

In this case, MVVM stands for – Model View ViewModel. As name suggests Model is nothing but classes representing data/entities. View represent actual view (pages in this context) whereas ViewModel does the binding between View and Model.

There are many frameworks out there which helps to build Windows Phone apps using MVVM pattern like Prism from Microsoft and MVVM Light from GalaSoft. In this blog, we’ll focus about MVVM Light.

First create a Windows Phone application.

image

Select Windows Phone 8.0 as OS Version in next dialog box.

Now, click on TOOLS > Library Package Manager > Manage NuGet Packages for Solution and search for MVVM Light in Online Gallery and install the same

image

When MVVM Light toolkit is installed, it modified App.Xaml file to instantiate the ViewModelLocator. These ViewModels are added in ViewModel folder.

image

MainViewModel is responsible to communicate between view and model. However, to keep things simple, we’ll expose a ApplicationName property which we’ll bind to Page.

image

Now, to bind ViewModel to View, in MainPage.xaml, add following code in phone:ApplicationPage element:

image

Now, bind ApplicationName to the textblock which displays App Name in MainPage.xaml

image

As soon as you modify above code, the UI changes and shows the Application Name. This is the beauty of using MVVM.

image

In next few blog-posts we’ll explore more about patterns and best practices in Windows Phone applications. Stay tuned! Smile

Resources:

Namaste

Mayur Tendulkar

Advertisements

Virtual Tech Conference 2013

South East Asia Microsoft Most Valuable Professionals are taking an initiative and hosting a virtual tech-conference. Best of the best speakers are going to talk about latest technology offerings from Microsoft. You can learn a lot from these sessions and you’ll also get an opportunity to ask questions if you’ve any. For more information, you can visit: http://virtualtechconference.azurewebsites.net/ for more information.

Capture

Even registration links are:

Dev Track – http://aka.ms/Ozhcyh
IT Pro Track – http://aka.ms/Uu9msj

Join us with this event and enjoy the learning.

Namste

Mayur Tendulkar

Using Raygun for Logging in Windows Phone Applications

There are very good error logging and reporting tools for other platforms. But when it comes to Windows Phone, developers need to depend on their own sources. Luckily, I got to know about Raygun.io and it is one of the best logging framework. In this post, we’ll see how we can use this platform to track, manage and report errors.

Special thanks to Mindscape for providing trial versions and details about this product. This error logging platform is available to other platforms as well like ASP.NET, PHP, JS, .NET, WinRT/WP and what not.

To use this platform, you’ll need to first create a Raygun.IO account. You can opt for a trial version. Once you login, you’ll be asked to create a new app or visit: https://app.raygun.io/application/create

image

Once the application is created, you’ll be taken to the Dashboard. On this dashboard you can see various platforms and your application key. Select Windows Phone platform.

image

Now create a Windows Phone application using Visual Studio

image

Using NuGet PackageManger (Tools > Library Package Manager > Manage NuGet Packages for solution) install Raygun required assemblies in Online category.

image

Now, in your Windows Phone’s App.xaml.cs file, create object of RaygunClient, passing your Application Key. This will require you include referred assemblies. Make a note of highlighted text.

So, the App.xaml.cs will look like:

 using System;  
using System.Diagnostics;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Mindscape.Raygun4Net;
using RaygunForWPDemo.Resources;
namespace RaygunForWPDemo
{
public partial class App
{
public static PhoneApplicationFrame RootFrame { get; private set; }
private readonly RaygunClient _client = new RaygunClient("YOUR APP KEY FROM REYGUN DASHBOARD");
public App()
{
UnhandledException += Application_UnhandledException;
InitializeComponent();
InitializePhoneApplication();
if (Debugger.IsAttached)
{
Current.Host.Settings.EnableFrameRateCounter = true;
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
}
}
#region Application Events
private void Application_Launching(object sender, LaunchingEventArgs e)
{
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
}
private void Application_Closing(object sender, ClosingEventArgs e)
{
}
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (Debugger.IsAttached)
{
Debugger.Break();
}
}
#endregion
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
_client.Send(e);
}
#region Phone application initialization
// Avoid double-initialization
private bool phoneApplicationInitialized = false;
// Do not add any additional code to this method
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Handle reset requests for clearing the backstack
RootFrame.Navigated += CheckForResetNavigation;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
// Do not add any additional code to this method
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{
// Set the root visual to allow the application to render
if (RootVisual != RootFrame)
RootVisual = RootFrame;
// Remove this handler since it is no longer needed
RootFrame.Navigated -= CompleteInitializePhoneApplication;
}
private void CheckForResetNavigation(object sender, NavigationEventArgs e)
{
// If the app has received a 'reset' navigation, then we need to check
// on the next navigation to see if the page stack should be reset
if (e.NavigationMode == NavigationMode.Reset)
RootFrame.Navigated += ClearBackStackAfterReset;
}
private void ClearBackStackAfterReset(object sender, NavigationEventArgs e)
{
// Unregister the event so it doesn't get called again
RootFrame.Navigated -= ClearBackStackAfterReset;
// Only clear the stack for 'new' (forward) and 'refresh' navigations
if (e.NavigationMode != NavigationMode.New && e.NavigationMode != NavigationMode.Refresh)
return;
// For UI consistency, clear the entire page stack
while (RootFrame.RemoveBackEntry() != null)
{
; // do nothing
}
}
#endregion
#region InitializeLanguage Code
private void InitializeLanguage()
{
try
{
// Set the font to match the display language defined by the
// ResourceLanguage resource string for each supported language.
//
// Fall back to the font of the neutral language if the Display
// language of the phone is not supported.
//
// If a compiler error is hit then ResourceLanguage is missing from
// the resource file.
RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage);
// Set the FlowDirection of all elements under the root frame based
// on the ResourceFlowDirection resource string for each
// supported language.
//
// If a compiler error is hit then ResourceFlowDirection is missing from
// the resource file.
FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection);
RootFrame.FlowDirection = flow;
}
catch
{
// If an exception is caught here it is most likely due to either
// ResourceLangauge not being correctly set to a supported language
// code or ResourceFlowDirection is set to a value other than LeftToRight
// or RightToLeft.
if (Debugger.IsAttached)
{
Debugger.Break();
}
throw;
}
}
#endregion
}
}

Now, we’ll add a button to MainPage and in the code behind we’ll write a method which will throw DivideByZeroException.

 private void Button_Click(object sender, RoutedEventArgs e)  
{
int num1 = 1;
int num2 = 0;
int answer = num1/num2;
MessageBox.Show(answer.ToString());
}

If we run the app and click on the button, it will throw an exception. This exception will be logged by Raygun and you can get its details on the dashboard like one shown below. You can see the status marked in Red blocks.

image

Apart from that, you’ll also receive an email with error details as shown below:

image

This platform can help you to track exceptions in your applications, which are already published on the store and used by consumers. This can allow you to fix these exceptions and release updates/patches to your app making your app more successful.

Do give Raygun.IO a try.

Namaste. Happy coding :)

Mayur Tendulkar

Building Windows Phone apps for SharePoint

Yes. I love Windows Phone and I (sometimes) hate SharePoint, I’ve my own personal reasons :)

But still, there are many enterprises who use SharePoint for various reasons like collaboration, document management and what not. Providing SharePoint access on mobile devices can result in high productivity for employees. Indeed, SharePoint does have Mobile interface exposed for every site, but look at the web-page and people will simply hate it even more.

 

(Image From TechNet)

So, why not make it beautiful by providing rich user interface? why not make it more productive by building customized SharePoint solutions and mobile application? Yes. We can.

When I tried building Windows Phone apps for SharePoint, the biggest hurdle was authentication as SharePoint gives various options like Claims/Forms/ACE, NTLM, MicrosoftOnline, etc.. And then calling REST APIs every now and then. Wictor Willen has written a nice blog about SharePoint online authentication and you can see how painful it is to make requests here and there just to do the authentication :( :)

But, recently I found out that Microsoft has released an SDK for building such apps. You can download these SDKs as: Microsoft SharePoint SDK for Windows Phone 7.1 & Microsoft SharePoint SDK for Windows Phone 8. Personally, I’ll recommend SDK for 7.1 unless you’re using any specific features from Windows Phone 8.

Once you download and install the SDK, using it is straight forward – just like using SharePoint CSOM. You need to refer following assemblies to use SharePoint APIs in your project:

  • Microsoft.SharePoint.Client.Phone.dll

  • Microsoft.SharePoint.Client.Phone.Runtime.dll

  • Microsoft.SharePoint.Client.Phone.Auth.UI

  • Microsoft.SharePoint.Phone.Application.dll

You can find these assemblies at: %ProgramFiles(x86)%\Microsoft SDKs\SharePoint\v15.0\Phone\%ver.%\

As you refer these libraries, accessing SharePoint list inside a Windows Phone is very simple:

image

In this example, it will show you a mobile authentication page (shown below) as I’m using MicrosoftOnline authentication mode. Else, you can provide login box to get credentials from users.

loginbox

After providing correct credentials, user will be authenticated and Failed/Succeeded callbacks will be sexecuted. Here you can process the results.

image

I hope this blog post will help you to build more SharePoint Mobile apps.

Namaste

Mayur Tendulkar

Global Windows Azure Boot-Camp Hosted by PUG

bootcamp

On April 27th, Microsoft and Communities hosted a Global Windows Azure BootCamp. Pune User Group also hosted this event and it was a grand success. With excellent in-depth topic coverage and pool of speakers, this event was well received. You can check the event pictures here on Facebook.

Along with my friend, Sarang Kulkarni, I presented a session on Windows Azure Mobile Services. You can find the presentation here

If you’ve missed this session, you can find more information about Windows Azure Mobile Services at http://www.windowsazure.com/en-us/develop/mobile/
Thanks a lot for making this event a big success. You can soon find more such events lined by from PUG. And hope to see you more at PUG Events.

58090_10151657913586584_824362951_n

Namaste

Mayur Tendulkar

Get Ready for Windows Phone 8 Development

With the this version of mobile operating system, Microsoft has introduced many new features in Windows Phone 8.0 devices. We’ve seen some of them here. Features for consumer, means APIs for developers. And Windows Phone provides many new APIs/Features/Tools which developers can benefit from.

If you’re a hobbyist developer, developing for Windows Phone is totally free. Please note: however, if you want to sell your apps and make money out of it, you need pay something. And in this post we’ll discuss all the necessary steps involved in developing Windows Phone 8.0 applications along with tools/SDKs required.

If you’re a Windows Phone 7 developer and have already built apps for it, your apps will run on Windows Phone 8.0 devices as it is. However, apps built for Windows Phone 8.0, will not work on earlier devices.

A Hyper-V Enabled, SLAT Enabled PC

Windows Phone 8.0 uses new emulation technique to provide better testing capabilities through emulator. To give device-like performance and other features, it requires Hyper-V support and SLAT support for your processor and motherboard. You can find more information about these requirements here. If you don’t have Hyper-V or SLAT support, you can still develop these apps; however, you’ll need a real device to test your code.

Windows 8 Or Windows 8 Pro (64bit)

Only 64bit client Operating System can run Windows Phone 8 Development Tools. You can get a trial version of Windows 8 from here OR if you’re a student, you can checkout your DreamSpark account.

Windows Phone 8 SDK

To develop apps for Windows Phone 8, you’ll need the current 8.0 SDK. Remember, SDK 7.1 can target only Windows Phone 7.0 (original launched devices) and 7.5 (Mango) devices. If you want to target 7.8 based devices, there is 7.8 SDK Update. However, SDK 8.0, allows you to build apps for all the versions. You can checkout all the SDKs here

Apart from these requirements, there are few other tools and SDKs, which can help you to be productive and earn some money by creating splendid looking phone apps. Let me put them here, later on we’ll dig dipper into these tools/libraries:

Along with these tools, I’ve found some really nice libraries, which can help you to expedite your Windows Phone (or for that matter any kind of app) development. You can use NuGet Package Manager console to add these packages:

  • Json.NET 4.5.11
    Json.NET is a popular high-performance JSON framework for .NET. To install follow the instructions as mentioned here
  • PhoneThemeManager
    This library allows you to force particular theme in your application like Dark/Light. Read more from the developer from here and to install follow the instructions as mentioned here

Definitely, you can use ReSharper and Reflector kind of tools to enhance your development experience.

I hope, this can be a good starting point for anyone who wants to start with Windows Phone Application Development. Try your hands on these tools and let me know. We’ll cover these tools/SDKs more in next few posts. Till then… happy coding Smile

Namaste

Mayur Tendulkar | www.mayurtendulkar.com

No water on moon? Blame Microsoft!!!

I had to write this post because many of my friends got influenced by some posts on some of the tech sites.

PLEASE NOTE: I’VE GATHERED SOME OF THE NOTES ON THIS POST FROM OTHER SITES AND PROVIDED LINKS TO THEM. IF OTHER SITES HAVE ANY ISSUES, I’LL REMOVE THEM. THESE NOTES AND COMMENTS FROM OTHER USERS ARE FOR REFERENCE ONLY.  JUST SEND ME A MAIL ON mayur.tendulkar {at} gmail.com

These days, media has decided to blame Microsoft for everything. Without thinking about the reason or the meaning of the statement made by someone. Media just makes it public in wrong way.

It is since ages. You got the blue screen? blame it on Microsoft. But, you’ll never understand that this blue screen can appear because of many reasons – incorrectly written driver, unsigned drivers, malfunctioning software. But nobody blame those developers, but Microsoft. For more details, visit this link: http://www.pcadvisor.co.uk/news/security/3435503/non-microsoft-security-flaws-the-ones-to-watch-secunia-analysis-finds/

As my friend Raj once told me, there were (and still valid now) rules about how to write apps for Windows. Where developers should put their setting/app files? Where developers should store roaming data and local data? And many more are there. People developed apps without following it. And media blamed Microsoft. So, here comes METRO Windows Store / Windows Phone App Development Guidelines. Follow it or leave it.

Recently, many sites posted about support for Windows Phone devices ending in 2014. Crap!!!

I’m going to highlight some of the comments from The Verge. You can find these comments here: http://www.theverge.com/2013/3/18/4118326/windows-phone-8-support-until-july-2014 

image

image

One post over there marks my current situation. I bought my Mozart in/around Dec’ 10. It still receives update. Rather, just yesterday I received one.

image

And one guy’s comment over here:

image

Unfortunately, these devices (7.0) can’t be upgraded to 8.0 because of kernel level changes. I can understand this one. But that is not valid for 8.0 device upgrades. I don’t see Microsoft will again ‘reimagine’ Windows Phone 8.0.

For sure: Microsoft can make things much more clear if they put right marketing around Windows Phone and make statements which are highly clear that nobody can take any other meaning from it.

image

And the last one, from The Verge itself: http://www.theverge.com/2012/12/27/3808858/htc-hd2-running-windows-RT 

image

Just have a guess about HTC HD2 specs, release date and dates for support end.

I think, that’s it. Rather than discussing on the support lifecycle, I would have loved if the discussion was around features and apps that are required on Windows Phone. 

By the way, Microsoft has the best support lifecycle than any other vendor over here. Imagine devices built for Windows XP Tablet Edition (Toshiba Tablet) running Windows 7 very smoothly.

— JUST MY THOUGHTS…

Namaste

Mayur Tendulkar | www.mayurtendulkar.com