简体   繁体   English

更新 Xamarin 中的 Label 文本

[英]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.我是 Xamarin 的新手,我正在尝试创建一个简单的应用程序来将我的移动图像上传到云端并从云端下载它们。 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.我有我想上传的图片数量,所以我想在我的 xaml 表单上创建一个 label 并在每张图片上传后更新它的值。 Example.例子。 if Total pictures are 5 and the loop used to upload the images have uploaded 2 pictures.如果图片总数为 5 并且用于上传图片的循环已上传 2 张图片。 I'll show "2 of 5 Images uploaded" as text in my label.我将在我的 label 中显示“上传的 5 张图片中的 2 张”作为文本。

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.一个是正如杰森所说,UploadClass 每次完成上传时都会引发一个事件。 This is like a CallBack method can be used in Mainfile.xaml.cs .(Recommanded way)这就像一个CallBack方法可以在Mainfile.xaml.cs中使用。(推荐方式)

For example, declare a UpdateLabelText Method in Mainfile.xaml.cs :例如,在Mainfile.xaml.cs中声明一个UpdateLabelText方法:

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

And raise method in UploadClass :并在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)另一种是使用MessageCenter通知Mainfile.xaml.cs更新Label 。(简单方法)

For example, Subscribe MessageCenter in Mainfile.xaml.cs:比如订阅Mainfile.xaml.cs中的MessageCenter

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 :并从UploadClassUploadToCloud完成方法发送消息:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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