java实现两个页面传递参数,通过多个页面传递参数

本文介绍如何在Xamarin应用的多个页面间传递数据,并最终保存为XML文件的方法。作者详细展示了使用自定义可序列化类,在不同页面中填充属性,并通过导航服务将对象传递到最后一页面进行序列化的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在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().MakeFileStream(); //stream from UWP using dependencyservice

using (StreamWriter sw = new StreamWriter(s, Encoding.UTF8))

{

XmlSerializer serializer = new XmlSerializer(typeof(MyRootElement));

serializer.Serialize(sw, mre);

}

}

}

}

如果您需要更多内容以便回答我的问题,请与我们联系 .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值