Skip to content

Instantly share code, notes, and snippets.

@clausjoergensen
Created August 6, 2011 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clausjoergensen/1129591 to your computer and use it in GitHub Desktop.
Save clausjoergensen/1129591 to your computer and use it in GitHub Desktop.
Attached Navigation Behaviours for WP7
using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.Phone.Controls;
namespace Rejseplanen.Assets
{
public class DependencyObjectExtension
{
public static readonly DependencyProperty NavigateToProperty =
DependencyProperty.RegisterAttached("NavigateTo",
typeof(Uri), typeof(DependencyObjectExtension),
new PropertyMetadata(null, OnNavigateToChanged));
public static Uri GetNavigateTo(DependencyObject d)
{
Uri uriSource = null;
if (Uri.TryCreate(d.GetValue(NavigateToProperty).ToString(), UriKind.Relative, out uriSource))
{
return uriSource;
}
return null;
}
public static void SetNavigateTo(DependencyObject d, Uri value)
{
d.SetValue(NavigateToProperty, value);
}
private static void OnNavigateToChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var listener = GestureService.GetGestureListener(d);
var oldCommand = e.OldValue as Uri;
if (oldCommand != null)
{
listener.Tap -= OnNavigateTo;
}
var newCommand = e.NewValue as Uri;
if (newCommand != null)
{
listener.Tap += OnNavigateTo;
}
}
private static void OnNavigateTo(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
var d = sender as DependencyObject;
var source = GetNavigateTo(d);
if (source != null)
{
(App.Current.RootVisual as PhoneApplicationFrame).Navigate(source);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment