【问题标题】:Passing an argument through multiple pages通过多个页面传递参数
【发布时间】:2017-03-04 00:19:14
【问题描述】:

我在 Xamarin 可移植类库(目标平台 UWP)中创建了一个应用程序,用户在每个页面中填写几个 TextBox。我需要将此信息保存在最后一页的 xml 文件中(单击按钮时),因此我需要将信息通过每一页传递到最后一页。我该怎么做?

这是我的可序列化类:

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;
            }
}

这是我的第一页:

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));
        }
    }
}

第二页看起来像一个有用的用户在这里建议的(我猜错了):

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));
        }
    }
}

在最后一页创建文件:

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);
            }
        }
    }
}

如果您需要更多内容来回答我的问题,请告诉我。

【问题讨论】:

    标签: c# serialization xamarin uwp parameter-passing


    【解决方案1】:

    您只需在第一页上创建一次 MyRootElement 实例。之后,继续使用后续页面的相同实例。

    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));
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-21
      • 2014-10-30
      • 2010-11-24
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多