使用Frame.Navigate(typeof(第2页))在c#windows phone应用程序中传递多个参数

使用Frame.Navigate(typeof(第2页))在c#windows phone应用程序中传递多个参数,c#,xaml,windows-phone-8,windows-phone,C#,Xaml,Windows Phone 8,Windows Phone,我正在创建一个基本的MCQ应用程序,其中2个MCQ以及选项显示在一个页面上,提交按钮在文本块的第二个页面上显示结果 我使用的是Frame.Navigate(typeof(Page2))导航方法,它可以传递一个参数,但不能将多个参数传递到下一页 这是我的密码: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServ

我正在创建一个基本的MCQ应用程序,其中2个MCQ以及选项显示在一个页面上,提交按钮在文本块的第二个页面上显示结果

我使用的是
Frame.Navigate(typeof(Page2))
导航方法,它可以传递一个参数,但不能将多个参数传递到下一页

这是我的密码:

    using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Popups;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace MCQ
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {

        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }


        private void button_Click(object sender, RoutedEventArgs e)
        {




            string ans1 = "";
            string ans2 = "";
            if (radioButton.IsChecked == true)
            {
                ans1 = "Answer 1 is correct";

            }
            else
            {
                ans1 = "Answer 1 is incorrect";
            }
            if (radioButton2.IsChecked == true)
            {
                ans2 = "Answer 2 is correct";

            }
            else
            {
                ans2 = "Answer 2 is incorrect";
            }


            Frame.Navigate(typeof(Page2), ans1, ans2);

            //textBlock3.Text = ans1 + " & " + ans2; ;

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {


        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用Windows.UI.Popups;
//空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkId=391641
名称空间MCQ
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
this.NavigationCacheMode=NavigationCacheMode.Required;
}
/// 
///当此页面即将显示在框架中时调用。
/// 
///描述如何到达此页面的事件数据。
///此参数通常用于配置页面。
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
//TODO:准备在此处显示的页面。
//TODO:如果应用程序包含多个页面,请确保
//通过注册来处理硬件后退按钮
//Windows.Phone.UI.Input.HardwareButtons.BackPressed事件。
//如果您正在使用某些模板提供的NavigationHelper,
//此事件已为您处理。
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
字符串ans1=“”;
字符串ans2=“”;
if(radioButton.IsChecked==true)
{
ans1=“答案1正确”;
}
其他的
{
ans1=“答案1不正确”;
}
如果(radioButton2.IsChecked==true)
{
ans2=“答案2正确”;
}
其他的
{
ans2=“答案2不正确”;
}
帧导航(typeof(Page2),ans1,ans2);
//textBlock3.Text=ans1+“&”+ans2;
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
}
}
}
我发现错误,无法将字符串转换为windows.ui.xaml.media。第二个参数即ans2上的navigation.transitioninfo

任何帮助都将不胜感激,或以另一种方式将参数传递到下一页。
谢谢

创建一个具有2个属性的新类,并将属性设置为文本块值。然后在导航时传递此对象

创建有效负载类:

public class Payload
{
 public string ans1 { get;set;}
 public string ans2 { get;set;}
}
Payload payload = new Payload();
payload.ans1 = textblock1.Text;
payload.ans2 = textblock2.Text;
然后填充有效负载类:

public class Payload
{
 public string ans1 { get;set;}
 public string ans2 { get;set;}
}
Payload payload = new Payload();
payload.ans1 = textblock1.Text;
payload.ans2 = textblock2.Text;
然后,当您调用Navigate时,按如下方式传递负载实例:

this.Frame.Navigate(typeof(SecondPage),payload);

创建一个具有2个特性的新类,并将特性设置为文本块值。然后在导航时传递此对象

创建有效负载类:

public class Payload
{
 public string ans1 { get;set;}
 public string ans2 { get;set;}
}
Payload payload = new Payload();
payload.ans1 = textblock1.Text;
payload.ans2 = textblock2.Text;
然后填充有效负载类:

public class Payload
{
 public string ans1 { get;set;}
 public string ans2 { get;set;}
}
Payload payload = new Payload();
payload.ans1 = textblock1.Text;
payload.ans2 = textblock2.Text;
然后,当您调用Navigate时,按如下方式传递负载实例:

this.Frame.Navigate(typeof(SecondPage),payload);

您是否尝试过创建一个包含所有变量的新类对象并传递该对象?就我个人而言,我总是使用字符串。如果我有几个参数,那么我有一个类似于URI查询字符串的表示:param1=value1¶m2=value2。请记住,您的参数对象必须是可序列化的,请参阅@SimonPrice有一个要点。最好实现singleton对象更好的选择是创建JSON对象并将其作为JSON字符串传递。这可以通过使用Newtsonsoft nuget软件包来实现。您是否尝试过创建一个包含所有变量的新类对象并传递该对象?就我个人而言,我总是使用字符串。如果我有几个参数,那么我有一个类似于URI查询字符串的表示:param1=value1¶m2=value2。请记住,您的参数对象必须是可序列化的,请参阅@SimonPrice有一个要点。最好实现singleton对象更好的选择是创建JSON对象并将其作为JSON字符串传递。这可以使用Newtsonsoft nuget软件包实现。