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.

Introduction

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.

Background

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);  

Solution

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:

    [KnownType(typeof(MyModel))]
    [DataContract]
    public class MyModel
    {
        [DataMember]
        public Guid ID { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        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.

License

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

Share

About the Author

M_Tamas
Engineer
Hungary Hungary
No Biography provided

Comments and Discussions

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