C# 更新Xamarin中的标签文本

C# 更新Xamarin中的标签文本,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我是Xamarin的新手,正在尝试创建一个简单的应用程序,将我的移动图像上传到云端并从云端下载。我可以从云端上传和下载文件。现在我想在上传/下载文件时显示一些进度状态。我有想要上传的图片的数量,所以我想在我的xaml表单上创建一个标签,并在每次图片上传后更新它的值。例子。如果总图片数为5,则用于上载图片的循环已上载2张图片。我将在我的标签中显示“上传的5张图片中的2张” 我的代码结构如下: 1. Mainfile.xaml <Label x:Name="UpdatedStatu

我是Xamarin的新手,正在尝试创建一个简单的应用程序,将我的移动图像上传到云端并从云端下载。我可以从云端上传和下载文件。现在我想在上传/下载文件时显示一些进度状态。我有想要上传的图片的数量,所以我想在我的xaml表单上创建一个标签,并在每次图片上传后更新它的值。例子。如果总图片数为5,则用于上载图片的循环已上载2张图片。我将在我的标签中显示“上传的5张图片中的2张”

我的代码结构如下:

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 
    }
 }
}
1。Mainfile.xaml
//我的标签显示状态
2.Mainfile.xaml.cs
//OnUploadClick的实施
私有异步void OnBackUpButton_已单击(对象发送方,事件参数e)
{
UploadClass upload=新建UploadClass();
等待上载。UploadImageFunction();//上载图像的函数引用
}
3.UploadClass.cs
//上传图像功能的实际实现
公共异步任务UploadImageFunction()
{ 
//获取要上载的图像计数的逻辑
foreach(TotalImages中的图像)
{
尝试
{
等待上传到云(图像);
/*如何从这里更新每次上传的标签。
我能够更新标签第二个文件,但不是从这个。
我在尝试类似的东西
UpdatedStatus.Text=“更新的文件1”;
但这是行不通的*/
}
捕获(例外)
{
//发生错误时的逻辑
}
}
}

提前感谢您的帮助。

有两种方法可以实现此目的

One正如Jason所说,UploadClass在每次完成上传时都会引发一个事件。这就像回调方法可以在
Mainfile.xaml.cs
中使用(推荐方式)

例如,在
Mainfile.xaml.cs
中声明一个
UpdateLabelText
方法:

public void UpdateLabelText(int index)
{
    //throw new NotImplementedException();
    label.Text = index + " of 5 Images uploaded" ;
}
并在上传类中提出方法:

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);
}
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);
}
另一个正在使用通知
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" ;
    });
}

允许UploadClass直接更新完全独立页面的UI将违反多个OO原则。一种更干净的方法是让UploadClass在每次完成上载时都引发一个事件,这样使用它的任何页面/类都可以根据自己的选择更新其UI(或不更新)。感谢您的解决方案。我尝试了这两种方法,发现更新的值正在传递到label.Text。但直到整个函数完成执行,标签文本才会更新。我只看到“上传5张图片中的5张”。我想为每个循环更新标签。示例:“上传了5张图像中的1张”,然后是“上传了5张图像中的2张”,如此类推。@Mystry需要
UploadClass
在每次上传图像后引发事件。您需要检查
UploadClass
是否具有此功能。@通常,您需要创建一个线程队列来上载图像。该线程队列包含五个线程任务,每个线程任务完成上载任务后,将通知其他类。更好的是,它们是同步的,直到前面的任务完成,下一个任务将继续。可以参考这个:我会看看这个链接。我使用的是MessagingCenter方法。但我认为这在for循环中不起作用。@Mystry还可以共享在何处发出消息的代码,需要检查它在何处调用了五次。