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

Clearer – A Gesture-Driven Windows 8 To-Do Application

Rate me:
Please Sign up or sign in to vote.
4.97/5 (14 votes)
9 Oct 2012CPOL15 min read 52.9K   878   22  
This article describes my experiences of porting a novel gesture-driven Windows Phone app to Windows 8.
using System;
using ClearStyle.ViewModel;
using LinqToVisualTree;
using System.Linq;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml;

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

    protected ItemsControl _todoList;
    protected ObservableCollectionEx<ToDoItem> _todoItems;
    private ScrollViewer _scrollViewer;

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

      IsEnabled = true;
    }

    protected ScrollViewer ScrollViewer
    {
      get
      {
        if (_scrollViewer == null)
        {
          _scrollViewer = _todoList.Descendants<ScrollViewer>()
                              .Cast<ScrollViewer>()
                              .SingleOrDefault();
        }
        return _scrollViewer;
      }
    }

    protected virtual void ScrollViewerLocated(ScrollViewer scrollViewer)
    {
    }

    public virtual void AddElement(FrameworkElement rootElement, FrameworkElement transformElement)
    {
    }

    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()
    {
      StackPanel itemsPanel = _todoList.Descendants<StackPanel>().OfType<StackPanel>().Single();

      var old = itemsPanel.ChildrenTransitions;
      itemsPanel.ChildrenTransitions = null;
      _todoItems.Reset();

      itemsPanel.InvokeOnNextLayoutUpdated(() =>
      itemsPanel.ChildrenTransitions = old);
    }
  }
}

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