简体   繁体   中英

Passing an argument through multiple pages

I have created an app in Xamarin portable class library (target platform UWP) where the user fills in a few TextBoxes in each page. I need to save this information in an xml file at the last page (on button click) and therefore I need to pass information through each page to the last one. How do I do that?

Here my serializable class:

namespace myProject
{
[XmlRoot("MyRootElement")]
        public class MyRootElement
        {
            [XmlAttribute("MyAttribute1")] //name of the xml element
            public string MyAttribute1     //name of a textboxt e.g.
            {
                get;
                set;
            }
            [XmlAttribute("MyAttribute2")]
            public string MyAttribute2
            {
                get;
                set;
            }
            [XmlElement("MyElement1")]
            public string MyElement1
            {
                get;
                set;
            }
}

Here is my first page:

namespace myProject
{
    public partial class FirstPage : ContentPage
    {
        public FirstPage()
        {
            InitializeComponent();
        }
        async void Continue_Clicked(object sender, EventArgs e)
        {
            MyRootElement mre = new MyRootElement
            {
                MyAttribute1 = editor1.Text,
                MyAttribute2 = editor2.Text,
                MyElement1 = editor3.Text
            };
            await Navigation.PushAsync(new SecondPage(mre));
        }
    }
}

Second Page looks like this as suggested by a helpful user here (I got it wrong I guess):

namespace myProject
{
    public partial class SecondPage : ContentPage
    {

        public MyRootElement mre { get; set; }

        public SecondPage(MyRootElement mre)
        {
            this.mre = mre;
            InitializeComponent();
        }

        async void Continue2_Clicked(object sender, EventArgs e)
        {
            MyRootElement mre = new MyRootElement
            {
                someOtherElement = editorOnNextPage.Text
            };
            await Navigation.PushAsync(new SecondPage(mre));
        }
    }
}

On the last page the file is created:

namespace myProject
{
    public partial class LastPage : ContentPage
    {
        private MyRootElement mre { get; set; }

        public LastPage(MyRootElement mre)
        {
            this.mre = mre;
            InitializeComponent();
        }

        private async void CreateandSend_Clicked(object sender, EventArgs e)
        {
            var s = await DependencyService.Get<IFileHelper>().MakeFileStream(); //stream from UWP using dependencyservice

            using (StreamWriter sw = new StreamWriter(s, Encoding.UTF8))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MyRootElement));
                serializer.Serialize(sw, mre);
            }
        }
    }
}

Please let me know if you need more content in order to answer my question.

You only need to create an instance of MyRootElement once, on your first page. After that, continue to use the same instance of subsequent pages.

async void Continue2_Clicked(object sender, EventArgs e)
{
  // use the same copy of mre you passed via the construt
  this.mre.someOtherElement = editorOnNextPage.Text

  await Navigation.PushAsync(new SecondPage(mre));
}

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