Windows Phone 7 在页面之间传递值

编程入门 行业动态 更新时间:2024-10-26 04:19:26
本文介绍了Windows Phone 7 在页面之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

可能的重复:
如何将一个 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.请记住,您不能将每个字符串都传递给您的 Uri - 如果它包含诸如 '&' 之类的字符,您的应用程序将会崩溃.这就是您必须使用 Uri.UnescapeDataString 的原因.

You can use JSON to serialize your array. You can use JSON 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(请参阅文档).请记住在序列化后使用 Uri.UnescapeDataString.

This an example for 2D string array. If you need to pass complex objects it's still possible to use JSON (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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 14:19:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   Windows   Phone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!