0

Possible Duplicate:
How to pass the image value in one xaml page to another xaml page in windows phone 7?
Passing data from page to page

I think is not a duplicate, because I dont found answer for my question, I dont passing an image, I passing 2darray[3,3] values in Page2. I am developing a Windows Phone 7 app. I have two pages (mainpage and page2). I wrote the code in the mainpage. I have a 2D array with values. How can I use this array in page2? Please give a step by step answer, I am a beginner.

Community
  • 1
  • 1

2 Answers2

2

You can use JSON to serialize your array. You can use JSON.net like I did. Remember that you cannot pass every string to your Uri - if it contains characters like '&' your app will crash. That's why you have to use Uri.UnescapeDataString.

This an example for 2D string array. If you need to pass complex objects it's still possible to use JSON.net (see the documentation). Just remember to use Uri.UnescapeDataString after serialization.

Before you deserialize your array from JSON you have to unescape it (Uri.UnescapeDataString).

In your source page:

using System;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;

namespace PhoneApp2
{
    public static class Extensions
    {

        public static string GetHtmlDecoded(this string str)
        {
            return HttpUtility.HtmlDecode(str);
        }

        public static string GetHtmlEncoded(this string str)
        {
            return HttpUtility.HtmlEncode(str);
        }

    }

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {


            var arrStr = new[,]
                {
                    {"aaaa$ffeaw&fewa=324&fewa", "fewa"},
                    {"aafw&fewa=324&fewa", "fefewa"},
                };


            string param = JsonConvert.SerializeObject(arrStr);
            param = Uri.EscapeDataString( param);

            var destination = new Uri("/Page1.xaml?arr=" + param, UriKind.Relative);
            NavigationService.Navigate(destination );
        }
    }
}

In the destination page:

using System;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;

namespace PhoneApp2
{
    public partial class Page1 : PhoneApplicationPage
    {
        public Page1()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            var param = Uri.UnescapeDataString(NavigationContext.QueryString["arr"]);
            var arr = JsonConvert.DeserializeObject<string[,]>(param);
        }
    }
}
Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130
  • `-1` for syntax errors, and not even addressing the issue of serializing a 2D array as a query string parameter. – Mike Christensen Feb 04 '13 at 17:49
  • @MikeChristensen You're right I was a little messy. I fixed it. :) – Andrzej Gis Feb 04 '13 at 19:00
  • @user2040397 btw the easiest way to reference to the JSON.net and many other libraries would be through Nuget. Read about it if haven't used it before. – Andrzej Gis Feb 04 '13 at 19:16
  • I installed Json package. I wrote it. But I dont know how to use my arrays in destination page. My arrays name: racs[3,3] and it is a string. So example I would like that: TextBox.Text = racs[1,2] in the destination Page. – user2040397 Feb 04 '13 at 19:49
  • @user2040397 You cannot call Text property on TextBox class. You have to call it on your object. Like that: myTextBox.Text=racs[1,2]; Comments is not the right place for this discussion. I suggest you read a tutorial on windows phone and if something is still unclear ask another question. – Andrzej Gis Feb 04 '13 at 20:36
0

I am not familiar with Windows Phone 7 (yet), but I would imagine it is possible to declare a public property on Page2 and populate it with the array after creation.

Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68