Windows Phone 7 在页面之间传递值 [英] Windows Phone 7 passing values between pages

查看:27
本文介绍了Windows Phone 7 在页面之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何将一个 xaml 页面中的图像值传递到 Windows Phone 7 中的另一个 xaml 页面?
在页面间传递数据

我认为不是重复的,因为我没有找到问题的答案,我没有传递图像,而是在 Page2 中传递 2darray[3,3] 值.我正在开发 Windows Phone 7 应用程序.我有两个页面(主页和第 2 页).我在主页上写了代码.我有一个带有值的二维数组.如何在 page2 中使用这个数组?请一步一步给出答案,我是初学者.

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.

推荐答案

您可以使用 JSON 来序列化您的数组.您可以像我一样使用 JSON.net.请记住,您不能将每个字符串都传递给您的 Uri - 如果它包含诸如 '&' 之类的字符,您的应用程序将会崩溃.这就是您必须使用 Uri.UnescapeDataString 的原因.

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.

这是二维字符串数组的示例.如果您需要传递复杂的对象,仍然可以使用 JSON.net(请参阅文档).请记住在序列化后使用 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.

从 JSON 反序列化数组之前,您必须对其取消转义 (Uri.UnescapeDataString).

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

在您的源页面中:

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

在目标页面:

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

这篇关于Windows Phone 7 在页面之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆