Click here to Skip to main content
Click here to Skip to main content

Complex Navigation Parameter in Metro Style App

, 27 Jan 2013
Rate this:
Please Sign up or sign in to vote.
This tip provides information on how to pass complex types as a parameter in Windows Metro style application.


Recently, I encountered a problem while trying to pass complex parameter during a page navigation in a Metro Style app:

public bool Navigate(Type sourcePageType, object parameter);

The problem is if you are using the built in template that contains those predefined navigation methods, then you are only able to send simple data types like: string, bool, decimal... as a parameter.


The problem occurs if you are using the predefined template provided by Visual Studio. In this case, all the basic pages have a common parent called LayoutAwarePage, inherited from the page class. The page class has a member called Frame that can handle the navigation queries:

public bool Navigate(Type sourcePageType, object parameter);  


If our parameter is a complex object and - as we saw - a string can be transferred as a parameter to another page, one solution can be to serialize our complex object into a string. Let's see first our object that needs to be serialized:

    public class MyModel
        public Guid ID { get; set; }

        public string Name { get; set; }

        public string Email { get; set; }

Let's write a generic class to make the serialization and deserialization:

 public class ObjectSerializer<T>
        public string Serialize<T>(object model)
            MemoryStream memStm = new MemoryStream();
            DataContractSerializer ser = new DataContractSerializer(typeof(T));
            StringWriter writer = new StringWriter();
            ser.WriteObject(memStm, model);
            memStm.Seek(0, SeekOrigin.Begin);
            string serilizedText = new StreamReader(memStm).ReadToEnd();
            return serilizedText;
        public T Deserialize(string text)
            using (Stream stream = new MemoryStream())
                byte[] data = System.Text.Encoding.UTF8.GetBytes(text);
                stream.Write(data, 0, data.Length);
                stream.Position = 0;
                DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
                return (T)deserializer.ReadObject(stream);

Usage of this generic class:

ObjectSerializer<MyModel> ser = new ObjectSerializer<MyModel>();
string serialized = ser.Serialize<ContactModel>(_myModelInstance);
MyModel deserialized = ser.Deserialize(serialzed);

Points of Interest

This tip can be used by anybody who would like to send a complex object parameter between pages in a Metro Style application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hungary Hungary
No Biography provided

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 28 Jan 2013
Article Copyright 2013 by M_Tamas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid