Обновлено:
Создайте модель TimeStamp с онлайн, автономным временем начала и окончания.
public class TimeStamp
{
public string OnlineStartTime { get; set; }
public string OnlineEndTime { get; set; }
public string OfflineStartTime { get; set; }
public string OfflineEndTime { get; set; }
}
MainPage:
Xaml:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="70" />
<RowDefinition Height="30" />
<RowDefinition Height="20" />
<RowDefinition Height="70" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="10" />
<RowDefinition Height="70" />
<RowDefinition Height="30" />
<RowDefinition Height="20" />
<RowDefinition Height="70" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="10" />
<RowDefinition Height="70" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="10" />
<RowDefinition Height="70" />
<RowDefinition Height="30" />
<RowDefinition Height="20" />
<RowDefinition Height="70" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="10" />
<RowDefinition Height="70" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="10" />
<RowDefinition Height="50" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="70" />
</Grid.ColumnDefinitions>
<!-- OFFLINE TOOL -->
<ImageButton
x:Name="btnOffline"
Grid.Row="1"
Grid.Column="1"
BackgroundColor="Transparent"
Clicked="btnOffline_Clicked"
IsEnabled="True"
Source="monkeyicon.png" />
<Label
Grid.Row="2"
Grid.Column="1"
Margin="15,0,0,0"
Text="Offline Tool" />
<Label
Grid.Row="1"
Grid.Column="3"
Text="Start Date Time:" />
<Label
Grid.Row="1"
Grid.Column="3"
Margin="0,40,0,0"
Text="End Date Time:" />
<Label
x:Name="lblStartDT"
Grid.Row="1"
Grid.RowSpan="2"
Grid.Column="4"
Grid.ColumnSpan="2"
Text="{Binding OfflineStartTime}" />
<Label
x:Name="lblEndDT"
Grid.Row="1"
Grid.RowSpan="2"
Grid.Column="4"
Grid.ColumnSpan="2"
Margin="0,40,0,0"
Text="{Binding OfflineEndTime}" />
<!-- ONLINE TOOL -->
<ImageButton
x:Name="btnOnline"
Grid.Row="8"
Grid.Column="1"
BackgroundColor="Transparent"
Clicked="btnOnline_Clicked"
IsEnabled="True"
Source="star_small.png" />
<Label
Grid.Row="9"
Grid.Column="1"
Margin="19,0,0,0"
Text="Online Tool" />
<Label
Grid.Row="8"
Grid.Column="3"
Text="Start Date Time:" />
<Label
Grid.Row="8"
Grid.Column="3"
Margin="7,40,0,0"
Text="End Date Time:" />
<Label
Grid.Row="9"
Grid.Column="3"
Margin="58,0,0,0"
Text="Status:" />
<Label
x:Name="lblOnlineStartDT"
Grid.Row="8"
Grid.RowSpan="2"
Grid.Column="4"
Grid.ColumnSpan="2"
Text="{Binding OnlineStartTime}" />
<Label
x:Name="lblOnlineEndDT"
Grid.Row="8"
Grid.RowSpan="2"
Grid.Column="4"
Grid.ColumnSpan="2"
Margin="0,40,0,0"
Text="{Binding OnlineEndTime}" />
<Label
x:Name="txtOnlineStatus"
Grid.Row="9"
Grid.Column="4"
Text="NOT STARTED"
TextColor="Red" />
</Grid>
Код:
public partial class MainPage : ContentPage
{
public string mainpagevalue;
int offlinecount = 0;
int onlinecount = 0;
public static TimeStamp timeStamp = new TimeStamp();
public MainPage()
{
InitializeComponent();
}
private async void btnOnline_Clicked(object sender, EventArgs e)
{
onlinecount++;
if (onlinecount == 1)
{
string currentDT = DateTime.Now.ToString();
lblOnlineStartDT.Text = currentDT;
timeStamp.OnlineStartTime = currentDT;
}
await Navigation.PushAsync(new OnlinePage());
}
private void btnOffline_Clicked(object sender, EventArgs e)
{
offlinecount++;
//Navigation.PushAsync(new SecondPage(this, lblEndDT));
Navigation.PushAsync(new OfflinePage(this, lblEndDT, btnOnline));
if (offlinecount == 1)
{
string currentDT = DateTime.Now.ToString();
lblStartDT.Text = currentDT;
timeStamp.OfflineStartTime = currentDT;
}
}
}
OfflinePage:
public partial class OfflinePage : ContentPage
{
Label offlineEndDT;
MainPage mainpage;
ImageButton btnonline;
public OfflinePage()
{
InitializeComponent();
}
public OfflinePage(MainPage mpage, Label endDT, ImageButton onlineimage)
{
InitializeComponent();
mainpage = mpage;
offlineEndDT = endDT;
btnonline = onlineimage;
}
private void Button_Clicked(object sender, EventArgs e)
{
App.offlineDonecount++;
if (App.offlineDonecount == 1)
{
string edt = DateTime.Now.ToString();
offlineEndDT.Text = edt;
mainpage.mainpagevalue = offlineEndDT.Text;
MainPage.timeStamp.OfflineEndTime = edt;
}
btnonline.IsEnabled = true;
Navigation.PopAsync();
}
}
OnlinePage:
private void Button_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new ThirdPage());
}
Третья страница:
private async void BtnDone_Clicked(object sender, EventArgs e)
{
MainPage.timeStamp.OnlineEndTime = DateTime.Now.ToString();
MainPage mainPage = new MainPage();
mainPage.BindingContext = MainPage.timeStamp;
await Navigation.PushAsync(mainPage);
}