简体   繁体   中英

Update Label text in Xamarin

I am new to Xamarin and am trying to create a simple app to upload my mobile images to cloud and download them from cloud. I was able to upload and download the files from cloud. Now I want to show some progress status, while uploading/downloading the files. I have the count of images i want to upload, so I was thinking to create a label on my xaml form and update its value after every picture upload. Example. if Total pictures are 5 and the loop used to upload the images have uploaded 2 pictures. I'll show "2 of 5 Images uploaded" as text in my label.

My code structure is like this:

1. Mainfile.xaml

<Label x:Name="UpdatedStatus" Text="No file to Update" />  // my label to show status
<Button Text="Upload Image" x:Name="btnUpload" Clicked="OnUploadClick" />

2. Mainfile.xaml.cs
//Implementation of OnUploadClick
private async void OnBackUpButton_Clicked(object sender, EventArgs e)
{
  UploadClass upload = new UploadClass();
  await upload.UploadImageFunction(); // reference of function to upload the image
}

3. UploadClass.cs
//actual implementation of function to upload image
public async Task UploadImageFunction()
{ 
  //logic to get the count of the images to upload
  foreach (image in TotalImages)
  {
    try
    {
       await UploadToCloud(images);
       /* How can I update up label from here for every upload. 
       I was able to update the label 2nd file but not from this. 
       I was trying something like
       UpdatedStatus.Text = "updated file 1"; 
       But this is not working.*/
    }
    catch (Exception)
    {
      // logic in case of any error 
    }
 }
}

Thanks in advance for help.

There are two options to achieve this.

One is as Jason's said, UploadClass raise an event every time it completes an upload. This is like a CallBack method can be used in Mainfile.xaml.cs .(Recommanded way)

For example, declare a UpdateLabelText Method in Mainfile.xaml.cs :

public void UpdateLabelText(int index)
{
    //throw new NotImplementedException();
    label.Text = index + " of 5 Images uploaded" ;
}

And raise method in UploadClass :

privat UploadComplete()
{
    ...
    // each image uploaded successfully send it's index
    // such as index == 1/2/3/4/5
    
   Mainfile mainfile = new Mainfile();
   mainfile.UpdateLabelText(1);
}

The another one is using MessageCenter to notify the Mainfile.xaml.cs to update Label .(Easy way)

For example, Subscribe MessageCenter in Mainfile.xaml.cs:

public Mainfile()
{
    InitializeComponent();
    
    MessagingCenter.Subscribe<object,int>(this, "Hi", (sender,arg) =>
    {
      // Do something whenever the "Hi" message is received
      label.Text = arg + " of 5 Images uploaded" ;
    });
}

and Send Message from UploadToCloud complete method of UploadClass :

privat UploadComplete()
{
    ...
    // each image uploaded successfully send it's index
    // such as index == 1/2/3/4/5
    
    MessagingCenter.Send<object, int>(this, "Hi", index);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM