martes, 8 de febrero de 2011

Función para paso de parámetros entre páginas Windows Phone 7 C#

Una de las formas para el paso de parámetros entre páginas en una aplicación para Windows Phone 7, es utilizando el área de almacenamiento aislado, en seguida coloco el código fuente para encapsular el paso de parámetros entre paginas en un par de funciones tipo SET y GET. 

Ver listado de código

using System;
using System.Net;
using System.IO.IsolatedStorage;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;


namespace F.Phone.IO
{
    public class PageParameters
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="targetPage"></param>
        /// <param name="parameters"></param>
        public static void SetParameters(string targetPage, Dictionary <string, string> parameters)
        {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            { 
                XDocument _doc = new XDocument();
                XElement _parameters = new XElement("parameters");
                foreach (KeyValuePair<string, string> p in parameters)
                {
                    XAttribute parameter = new XAttribute(p.Key, p.Value);
                    _parameters.Add(parameter);
                }

                _doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), _parameters);
                IsolatedStorageFileStream filepath = new IsolatedStorageFileStream(  targetPage + ".parameters", System.IO.FileMode.Create, storage);
                System.IO.StreamWriter file = new System.IO.StreamWriter(filepath);
                _doc.Save(file);
                file.Dispose();
                filepath.Dispose();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="targetPage"></param>
        /// <returns></returns>
        public static Dictionary <string, string> GetParameters(string targetPage) {
             Dictionary <string, string> result=new Dictionary <string, string> ();
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                XElement _xml;
                IsolatedStorageFileStream filepath = new IsolatedStorageFileStream(targetPage +".parameters", System.IO.FileMode.Open, storage);
                System.IO.StreamReader file = new System.IO.StreamReader(filepath);
                _xml = XElement.Parse(file.ReadToEnd());
                if (_xml.Name.LocalName != null)
                {
                    foreach (XAttribute att in _xml.Attributes()) {
                        if (!result.ContainsKey (att.Name.ToString () )){
                            result.Add(att.Name.ToString(),att.Value);
                        }
                    }
                }
                file.Dispose();
                filepath.Dispose();
            }
            return result;
        }


    }
}


Transacciones Fiori

  /UI2/CACHE Register service for UI2 cache use /UI2/CACHE_DEL Delete cache entries /UI2/CHIP Chip Registration /UI2/CUST Customizing of UI ...