Click here to Skip to main content
15,886,518 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.6K   878   22  
This article describes my experiences of porting a novel gesture-driven Windows Phone app to Windows 8.
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;

namespace ClearStyle
{
  public static class Util
  {
    public static void Animate(this DependencyObject target, double? from, double to,
                              string propertyPath, int duration, int startTime,
                              EasingFunctionBase easing = null, Action completed = null)
    {
      if (easing == null)
      {
        easing = new SineEase();
      }

      var db = new DoubleAnimation();
      db.To = to;
      db.From = from;
      db.EasingFunction = easing;
      db.Duration = TimeSpan.FromMilliseconds(duration);
      Storyboard.SetTarget(db, target);
      Storyboard.SetTargetProperty(db, propertyPath);

      var sb = new Storyboard();
      sb.BeginTime = TimeSpan.FromMilliseconds(startTime);

      if (completed != null)
      {
        sb.Completed += (s, e) => completed();
      }

      sb.Children.Add(db);
      sb.Begin();
    }

    public static void SetHorizontalOffset(this FrameworkElement fe, double offset)
    {
      var translateTransform = fe.RenderTransform as TranslateTransform;
      if (translateTransform == null)
      {
        // create a new transform if one is not alreayd present
        var trans = new TranslateTransform()
        {
          X = offset
        };
        fe.RenderTransform = trans;
      }
      else
      {
        translateTransform.X = offset;
      }
    }

    public static Offset GetHorizontalOffset(this FrameworkElement fe)
    {
      var trans = fe.RenderTransform as TranslateTransform;
      if (trans == null)
      {
        // create a new transform if one is not alreayd present
        trans = new TranslateTransform()
        {
          X = 0
        };
        fe.RenderTransform = trans;
      }
      return new Offset()
      {
        Transform = trans,
        Value = trans.X
      };
    }

    public static void SetVerticalOffset(this FrameworkElement fe, double offset)
    {
      var translateTransform = fe.RenderTransform as TranslateTransform;
      if (translateTransform == null)
      {
        var trans = new TranslateTransform()
        {
          Y = offset
        };
        fe.RenderTransform = trans;
      }
      else
      {
        translateTransform.Y = offset;
      }
    }

    public static Offset GetVerticalOffset(this FrameworkElement fe)
    {
      var trans = fe.RenderTransform as TranslateTransform;
      if (trans == null)
      {
        trans = new TranslateTransform()
        {
          Y = 0
        };
        fe.RenderTransform = trans;
      }
      return new Offset()
      {
        Transform = trans,
        Value = trans.Y
      };
    }

    public struct Offset
    {
      public double Value { get; set; }
      public TranslateTransform Transform { get; set; }
    }

    public static void InvokeOnNextLayoutUpdated(this FrameworkElement element, Action action)
    {
      EventHandler<object> handler = null;
      handler = (s, e2) =>
      {
        element.LayoutUpdated -= handler;
        action();
      };
      element.LayoutUpdated += handler;
    }
  }
}

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