Created
August 31, 2012 07:27
-
-
Save Nilzor/3549872 to your computer and use it in GitHub Desktop.
Page navigation helpers Metro
This file contains 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
public class PageBase : LayoutAwarePage | |
{ | |
protected override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e) | |
{ | |
base.OnNavigatedTo(e); | |
object obj = null; | |
Debug.Assert(e.Parameter == null || e.Parameter is String, "Expected string as parameter"); | |
if (e.Parameter != null) obj = SerializationHelper.DataContractDeserialize<object>((string) e.Parameter); | |
OnNavigatedToSmart(e, obj); | |
} | |
/// <summary> | |
/// Triggered after OnNavigatedTo, where parameter is deserialized for you | |
/// </summary> | |
/// <param name="e"></param> | |
/// <param name="parameter"></param> | |
protected virtual void OnNavigatedToSmart(Windows.UI.Xaml.Navigation.NavigationEventArgs e, object parameter) { } | |
/// <summary> | |
/// Navigates to the page specified and serializes argument if any | |
/// </summary> | |
/// <param name="pageType"></param> | |
/// <param name="argument">Serializable argument object or null</param> | |
protected void NavigateToSmart(Type pageType, IMobileObject argument = null) | |
{ | |
string argAsString = null; | |
if (argument != null) argAsString = SerializationHelper.DataContractSerialize(argument); | |
Frame.Navigate(pageType, argAsString); | |
} | |
} |
This file contains 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
public class SerializationHelper | |
{ | |
public static string DataContractSerialize(object obj) | |
{ | |
using (var tempStream = new MemoryStream()) | |
{ | |
DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); | |
serializer.WriteObject(tempStream, obj); | |
tempStream.Seek(0, SeekOrigin.Begin); | |
StreamReader sr = new StreamReader(tempStream); | |
string objAsString = sr.ReadToEnd(); | |
return objAsString; | |
} | |
} | |
public static T DataContractDeserialize<T>(string s) | |
{ | |
var serializer = new DataContractSerializer(typeof(T)); | |
using (var tempStream = new MemoryStream()) | |
{ | |
var sr = new StreamWriter(tempStream); | |
sr.Write(s); | |
sr.Flush(); | |
tempStream.Seek(0, SeekOrigin.Begin); | |
return (T) serializer.ReadObject(tempStream); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment