Click here to Skip to main content
15,892,737 members
Articles / Mobile Apps / Windows Phone 7

A Gesture-Driven Windows Phone To-Do List

Rate me:
Please Sign up or sign in to vote.
4.94/5 (63 votes)
6 Sep 2012CPOL23 min read 141.1K   2K   83  
This article describes the development of a Windows Phone to-do list application that eschews buttons and checkboxes in favour of gestures.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using ClearStyle.ViewModel;
using LinqToVisualTree;
using System.Linq;

namespace ClearStyle.Interactions
{
  /// <summary>
  /// A base class for interactions.
  /// </summary>
  public abstract class InteractionBase : IInteraction
  {
    private bool _isActive = false;

    protected ItemsControl _todoList;
    protected ResettableObservableCollection<ToDoItemViewModel> _todoItems;
    protected ScrollViewer _scrollViewer;

    public virtual void Initialise(ItemsControl todoList, ResettableObservableCollection<ToDoItemViewModel> todoItems)
    {
      _todoList = todoList;
      _todoItems = todoItems;

      // when the ItemsControl has been rendered, we can locate the ScrollViewer
      // that is within its template.
      _todoList.InvokeOnNextLayoutUpdated(() => LocateScrollViewer());

      IsEnabled = true;
    }

    private void LocateScrollViewer()
    {
      _scrollViewer = _todoList.Descendants<ScrollViewer>()
                              .Cast<ScrollViewer>()
                              .Single();

      // allow interactions to perform some action when the ScrollViewer has been located
      // such as add event handlers
      ScrollViewerLocated(_scrollViewer);
    }

    protected virtual void ScrollViewerLocated(ScrollViewer scrollViewer)
    {
    }

    public virtual void AddElement(FrameworkElement rootElement)
    {
    }

    public bool IsActive
    {
      get
      {
        return _isActive;
      }
      set
      {
        _isActive = value;

        if (_isActive == true)
        {
          if (Activated != null)
          {
            Activated(this, EventArgs.Empty);
          }
        }
        else
        {
          if (DeActivated != null)
          {
            DeActivated(this, EventArgs.Empty);
          }
        }
      }
    }

    public bool IsEnabled { get; set; }
    
    public event EventHandler Activated;

    public event EventHandler DeActivated;

    /// <summary>
    /// Some interactions involve adding transformations or performing other visual modifications
    /// to items within the list. When the interaction is complete, we need to remove these and return
    /// the list to its original state. This method simply forces the ItemsControl to re-render all items.
    /// </summary>
    protected void RefreshView()
    {
      _todoItems.Reset();
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt

-

Comments and Discussions