MOBILE

Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 2) - Coding MainPage

1/18/2011 3:48:40 PM

3. Coding MainPage

In Solution Explorer, open MainPage.xaml.cs and replace the code there with the following C# code blocks that will implement the UI interacting with the user to add, delete, view, and edit notes, and also to register the user for the first time.

3.1. Specifying the Namespaces

Begin by listing the namespaces the application will use.

using System.Windows;
using Microsoft.Phone.Controls;

3.2. Code Constructor

In the constructor of MainPage, you will be setting DataContext of the user controls to the NotepadViewModel instance. When DataContext of ucNoteList and ucUserRegistraton is set to NotepadViewModel, the controls within the user controls' values will be controlled by the properties of NotepadViewModel.

public MainPage()
{
InitializeComponent();

this.DataContext = NotepadViewModel.Instance;
ucNoteList.DataContext = NotepadViewModel.Instance;
ucUserRegistration.DataContext = NotepadViewModel.Instance;
}

3.3. Coding the Save Button Event

When the user clicks the Add button, the SaveNote method from the NotepadViewModel instance will be called. Any direct calls to NotepadService will be handled from NotepadViewModel, leaving the handling of the web service call complexity centralized to NotepadViewModel. This is a great abstraction technique, allowing you to easily maintain the application.

private void btnSave_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtNote.Text))
{
NotepadViewModel.Instance.SaveNote(txtNoteName.Text, txtNote.Text);
}
}

3.4. Coding the ViewEdit Button Event

When the ViewEdit button is clicked, the ShowNoteList property in NotepadViewModel will be set to true, which will trigger NoteListUserControl to appear. ShowNoteList will be set to true only if there are Notes to be selected.

private void btnViewEdit_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtNote.Text))
{
NotepadViewModel.Instance.SaveNote(txtNoteName.Text, txtNote.Text);
}
}

3.5. Coding the AddNew Button Event

When the AddNew button is clicked, SelectedNode in NotepadViewModel will be set to null, triggering the txtNote and txtNoteName contents to be set to empty because they are bound to SelectedNote. Although you can directly set the Text fields of txtNote and txtNoteName to an empty string, we are abstracting this particular task to NotepadViewModel because when the user selects the specific user note from NoteListUserControl, the txtNote and txtNoteName content will be automatically changed because they are bound to SelectedNote.

private void btnAddNew_Click(object sender, System.Windows.RoutedEventArgs e)
{
NotepadViewModel.Instance.SelectedNote = null;
}

3.6. Coding the Delete Button Event

When the Delete button is clicked, the DeleteNote method from the NotepadViewModel instance will be invoked, SelectedNode will be set to null, and txtNote and txtNoteName will be set to an empty string automatically because they are bound to SelectedNode.

private void btnDelete_Click(object sender, System.Windows.RoutedEventArgs e)
{
NotepadViewModel.Instance.DeleteNote();
}

Other  
  •  Building Android Apps : Detecting Browsers with WURFL
  •  Building Android Apps : Submitting Your App to the Android Market - Preparing a Release Version of Your App
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 2) - Implementing a WCF Service to Access the SQL Azure Database
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 1) - Generating an Object Model to Access the Cloud Database
  •  Windows Phone 7 Development : Using Cloud Services As Data Stores - Creating a Cloud Database
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 5) - Implementing the View Controller Logic
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 4) - Hiding the Keyboard
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 3) - Creating Styled Buttons
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 2) - Adding Text Views
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 1) - Adding Text Fields
  •  
    Most View
    So What Is ‘Cloud’ And Why Should I Care? (Part 2)
    PowerShell for SharePoint 2013 : Word Automation Services - Configure the Conversion Processes, Configure Conversion Throughput
    Windows Vista : Logon and Profile Options (part 1)
    Top Ten Raspberry Pi Projects (Part 1)
    Olympus M.Zuiko Digital ED 9-18mm f/4-5.6 Lens
    Sandisk ReadyCache 32GB - Optimum Your Space To Cache
    How To Choose A Printer That Fits Your Demand
    Windows Server 2003 : Configuring a Windows IPSec Policy (part 1) - Using the IPSec Policy Wizard to Create a Policy
    Buying Guide: High-end CPUs (Part 2) - AMD FX-8150, Intel Core i7-2700K, AMD FX-8350
    Mymemory.com - Calendars And Picture Books Review (Part 2)
    Top 10
    Windows Server 2008 R2 : Active Directory certificate services (part 2) - Deploying Active Directory Certificate Services
    Windows Server 2008 R2 : Active Directory certificate services (part 1) - Planning for Active Directory Certificate Services
    Windows Server 2008 R2 : Administering group policy (part 2) - Creating and managing Group Policy Objects, Troubleshooting Group Policy
    Windows Server 2008 R2 : Administering group policy (part 1) - Overview of Group Policy
    Windows Server 2008 R2 : Administering groups and organizational units
    Microsoft Exchange Server 2010 : Creating and Working with Public Folders (part 3) - Adding Items to Public Folders Using Outlook
    Microsoft Exchange Server 2010 : Creating and Working with Public Folders (part 2) - Determining Public Folder Size, Item Count, and Last Access Time
    Microsoft Exchange Server 2010 : Creating and Working with Public Folders (part 1)
    Microsoft Exchange Server 2010 : Accessing Public Folders Through the Information Store
    SQL Server :ONE-WAY ENCRYPTION - Creating the Interface (part 2) - Setting and Verifying Permissions to the Stored Procedures