【发布时间】:2020-03-06 03:51:10
【问题描述】:
在我的主页上,当用户单击按钮时,我会将标签文本 (#lblStartDateTime) 设置为当前时间戳。它将导航到第二页,一旦我单击“完成”按钮,它将返回主页。
当我从第二页导航回主页时,我的标签文本消失了。有谁知道导航后如何保留标签文本值?
主页
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Test
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage: ContentPage
{
public string previouspagevalue;
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
lblEndDT.Text = previouspagevalue;
}
private void btnOffline_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new SecondPage());
string currentDT = DateTime.Now.ToString();
lblStartDT.Text = currentDT;
}
}
}
第二页
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Test
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SecondPage: ContentPage
{
public SecondPage()
{
InitializeComponent();
}
protected void btnDone_Clicked(object sender, EventArgs e)
{
MainPage mainpage = new MainPage();
string edt = DateTime.Now.ToString();
lblEndDateTime.Text = edt;
mainpage.previouspagevalue = lblEndDateTime.Text;
Navigation.PushAsync(mainpage);
}
}
}
【问题讨论】:
-
你能分享一些关于这个问题的代码吗? mainPage 中有关 lblStartDateTime 的代码。
-
嗨,我已经更新了我的代码! :)
标签: xaml xamarin xamarin.forms