Created
August 6, 2011 18:15
-
-
Save clausjoergensen/1129591 to your computer and use it in GitHub Desktop.
Attached Navigation Behaviours for WP7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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