Windows Phone 7.1 - Codeless Development using Commanding
Posted by: Mahesh Sabnis
in Category Windows Phone
Abstract: If you are planning to build Line-Of-Business applications for WP7, you can think of implementing the Commanding approach in your applications. Code-less development is a nice benefit provided by Commanding, so the UI can be flexible and need not be compiled if any of the business logic needs to be changed. In the example shown in this article, I have used a simple mechanism to implement commanding in WP7 applications.
With the Windows Phone 7.1 Mango Update, we can use a concept called Commanding. This helps in implementing code-less XAML (no-code behind). All of you who are thinking to make use of the Windows Phone platform for Line-of-Business (LOB) applications, can think of using the Commanding feature. I have already explained the use of commanding in Silverlight 4 over here.
In case of an LOB application, we often prefer to develop flexible UI with no or less dependency on the code-behind. Commanding is ideal in this scenario. As a developer, you need to create a command class and a support class (some documents also call it as ViewModel class) - here I am referring to it as the Adapter class. The responsibility of this class is to make call to an external system, which may be some DataStore/DataAccess classes or they can also be WCF service adapters. In the example shown in this article, I have used a simple mechanism to implement commanding. You can think of extending it as per your need.
Step 1: Open VS2010 and create a Silverlight Windows Phone application, make sure that you select the 7.1 version, and name it as ‘WP7_Commanding’.
Step 2: In the project add a new class file, name it as ‘CommandingClasses.cs’. This class file will contain the DataStore class, Adapter class and also the command class as shown below:
In the code shown above, the Employee class is used as a record class for Employee information. You can get this class from WCF Data Contracts. The EmployeeDataStore class is used to define Employee collection. The method for making all Employee information available to the Adapter class is by using ‘GetEmployees()’ method. The EmployeeAdapter and EmployeeCommand classes are the most important classes. Adapter class makes call to the EmployeeDataStore which makes a call to ‘GetEmployees()’ method. This class also defines a public property ‘EmpCollection’, which will be bound with the UI. The EmpCommand property of the type ICommand is bound with the UI. This command property accepts action from the UI and delivers it to the adapter class. To complete the execution, the EmployeeCommand class must check whether the action requested by the UI can be executed or not using CanExecute method. If the CanExecute returns true, the Execute method from the Command class gets executed and then the call to Adapter class is completed and the result is returned to the UI.
Step 3: Right click the project and add a new Windows Phone User Control and name it as ‘EmployeesUserControl.xaml’. This user control defines UI for displaying Employee information. This creates an object of the EmployeeAdapter class using XAML. Also bind the Command property exposed by the EmployeeAdapter class to the Button. The Design and the XAML code is as below:
Task 4: Design the MainPage.Xaml as below shown, add a Grid control of the name grdMain which will contain the user control and also add a button, that programmatically adds the user control in the Grid hrgMain.
Step 5: In the MainPage.Xaml.cs, write the following code on the click event of the ‘Load Employees View’:
Task 6: Run the Application and you should see the following results:
Conclusion: If you are planning to build Line-Of-Business applications for WP7, you can think of implementing the Commanding approach. Code-less development is a nice benefit provided by Commanding, so the UI can be flexible and need not be compiled if any of the business logic needs to be changed.
The entire source code of this article can be downloaded over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!