简体   繁体   中英

Send Parameters using Frame.Navigate in UWP

A class to temporarily store my input value here.

namespace App2
{
    class methC
    {
        public string tb1 { get; set; }
        public string tb2 { get; set; }
        public string tb3 { get; set; }
        public string tb4 { get; set; }
        public string tb5 { get; set; }
        public string tb6 { get; set; }

        public methC(string tb1, string tb2, string tb3)
        {
            this.tb1 = tb1;
            this.tb2 = tb2;
            this.tb3 = tb3;
            //this.right1 = right1;
            //this.right2 = right2;
            //this.right3 = right3;
            //this.Box = Box;
            //this.Grade = Grade; 
        }
    }
}

first page that take in the parameter

namespace App2
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
            Frame.Navigate(typeof(BlankPage1));
        }


    }
}

How to get the data stored in tb1,tb2,tb3 to display in BlankPage1?

namespace App2
{
    public sealed partial class BlankPage1 : Page
    {
        public BlankPage1()
        {
            this.InitializeComponent();


        }

        private void btn2_Click(object sender, RoutedEventArgs e)
        {

            string four, five, six;
            four = tb4.Text;
            five = tb5.Text;
            six = tb6.Text;
            box.Text = four + five + six;
        }

        private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }
    }

}

How's the thing suppose to work, MainPage take in 3 entry, store in methC and get from BlankPage to display tb1,tb2,tb3. How can i make this thing work?

As long as you Navigate to another page using a Type parameter you can't use a constructor to pass the data. so Microsoft provided another way of doing that work.

You can pass your object using this overload of Navigate function:

        methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
        string Param = JsonConvert.SerializeObject(mc);
        Frame.Navigate(typeof(BlankPage1), Param);

override OnNavigatedTo in the BlankPage1:

 protected override void OnNavigatedTo(NavigationEventArgs e)
 {
     methC myData = JsonConvert.DeserializeObject<methC>((string)e.Parameter);

     Labelx.Text = myData.x;
     , ....

     base.OnNavigatedTo(e);
 }

this is from MSDN

and the worst case is to assign them through static members, so in the BlankPage1 class you can add:

 public static methC Data { get; set; } 

 public BlankPage1()
 {
      mylabel.Text = Data.X;
      , ....
 } 

and you can assign the values before Navigation:

        methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
        BlankPage1.Data = mc;
        Frame.Navigate(typeof(BlankPage1));

In your mainpage, you can pass the parameter of the class object directly.

methC mc = new methC(tb1.Text, tb2.Text, tb3.Text);
Frame.Navigate(typeof(BlankPage1), mc);

In your next page, you can receive it as a class object

protected override void OnNavigatedTo(NavigationEventArgs e) {
        navigationHelper.OnNavigatedTo(e);
        methC item = e.Parameter as methC;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM