【问题标题】:Passing multiple parameters in c# windows phone app using Frame.Navigate(typeof(Page2))使用 Frame.Navigate(typeof(Page2)) 在 c# windows phone 应用程序中传递多个参数
【发布时间】:2017-08-11 18:27:37
【问题描述】:

我正在创建一个基本的 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)
        {


        }
    }
}

我得到错误无法从字符串转换为 windows.ui.xaml.media。 navigation.transitioninfo 关于第二个参数,即 ans2。

任何帮助或将参数传递到下一页的替代方法将不胜感激。 谢谢!

【问题讨论】:

  • 您是否尝试过创建一个包含所有变量的新类对象并传递该对象?
  • 就个人而言,我总是使用字符串。如果我有几个参数,我有一个类似于 URI 查询字符串的表示:param1=value1&param2=value2。请记住,您的参数对象必须是可序列化的,请参阅docs.microsoft.com/en-us/uwp/api/…
  • @SimonPrice 有道理。最好实现单例对象
  • 更好的选择是创建 JSON 对象并将其作为 json 字符串传递。这可以使用 Newtsonsoft nuget 包来实现。

标签: c# xaml windows-phone-8 windows-phone


【解决方案1】:

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

创建负载类:

public class Payload
{
 public string ans1 { get;set;}
 public string ans2 { get;set;}
}

然后填充payload类:

Payload payload = new Payload();
payload.ans1 = textblock1.Text;
payload.ans2 = textblock2.Text;

然后,当您调用 Navigate 时,像这样传递您的有效负载实例:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多