1
votes

How do I update my label text in main page, on button click event at another content page?

Example: When user click on image button, I will update the current time stamp in lblStartDT. The image button will redirect the user to another page, and when the user click "done" button, I want to update my lblEndDT with the current time stamp back at the main page. I don't know how to call the label from one page to another.

(P.S this is my first time using xamarin forms so I am not sure what approach I can use)

<Label Text="Start Date Time:" Grid.Row="1" Grid.Column="3"/>
<Label Text="End Date Time:" Grid.Row="1" Grid.Column="3" Margin="7,40,0,0"/>
<Label x:Name="lblStartDT" Text="-" Grid.Column="4" Grid.Row="1"/>
<Label x:Name="lblEndDT" Text="-" Grid.Column="4" Grid.Row="1" />

MainPage code

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 workorderpage : ContentPage
{
    public workorderpage()
    {
        InitializeComponent();
    }

private void btnOffline_Clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new OfflineToolPage());
        txtStatus.Text = "IN PROGRESS";

        string currentDT = DateTime.Now.ToString();
        lblStartDT.Text = currentDT;


    }

}

Second Page

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 OfflineToolPage : ContentPage
{
    public OfflineToolPage()
    {
        InitializeComponent();
    }


    private void btnDone_Clicked(object sender, EventArgs e)
    {
       #I want to call the label here
    }
}
1
Show us a bit more of your code, both windows and the code where you call the second page.Presi
Hi I have updated my post with my code. I would like to call the label in second page under btnDone clicked.Charis
Create Action in OfflineToolPage and register that action after instantialtion of the new OfflineToolPage() and then push the page. Invoke that action in btnDone_Clicked eventKrishna V

1 Answers

0
votes

You have different approaches that you can easily use to get what you need, here is the list.

1 - You can use Xamarin Forms Messaging Center that will allow you to use an observer like pattern , to register for particular events https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center This is useful if you have to react to the same event in different places, or if your architecture is too much complicated.

2 - If you are using navigation in the proper way you will be able to send parameters between difference pages. For example if you are using a navigation library as Prism here is your solution https://prismlibrary.com/docs/xamarin-forms/navigation/passing-parameters.html This is useful if your changes are point to point, like in your example, where pageA should react to a change in pageB.

3 - If you need to store your change between different lifecylec of the application, so even if the app will be closed, you can store the value in your appSettings. Here you can find a way to solve the problem in this way https://docs.microsoft.com/en-gb/xamarin/xamarin-forms/app-fundamentals/application-class#Properties_Dictionary

I think that these are the common approaches to solve your problem, I hope that this will help you ;)