来自静态方法的 MVVM 绑定

MVVM binding from static method

我正在开发我的应用程序,它将从在线数据库中获取数据。我想显示进度条。问题是我无法让它工作。

目前情况是:

问题是我发现的唯一方法是根据新实例更新此 属性。但是 MVVM 不起作用 --> UI 没有刷新,因为它更新了 属性 的不同实例。如何做到这一点?

下面是我的代码(如果有帮助的话)。

从云端检索数据库的代码

namespace iVanApp.Services {
public class FirebaseCloudNight : FirebaseCloudBaseHelper
{
    
    static public async Task<bool> SynchronizeNights()
    {
        // Synchronize from Cloud to DB
        foreach (var cloudItem in cloudNights)
        {
            CurrentProgress= currentNight / numberOfNights;
            try
            { 
                //Here I removed code for retriving data from online DB
                currentNight ++;
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
     }
 }

ViewModel 代码

 async Task GetData()
    {
            bool success = await FirebaseCloudNight.SynchronizeNights();>                 
    }

    float currentProgress;
    public float CurrentProgress
    {
        get => currentProgress;
        set
        {
            currentProgress = value;
            RaisePropertyChanged();
        }
    }

编辑: 我尝试过但没有用(原因很明显)

namespace iVanApp.Services {
public class FirebaseCloudNight : FirebaseCloudBaseHelper
{
    
    static public async Task<bool> SynchronizeNights()
    {
        // Synchronize from Cloud to DB
        var VM = new UserAuthenticationPageModel();
        foreach (var cloudItem in cloudNights)
        {
            VM.CurrentProgress = currentNight / numberOfNights;
            try
            { 
                //Here I removed code for retriving data from online DB
                currentNight ++;
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
     }
 }

找到有效的答案

在后面的 ViewModel 代码中添加了代码

public class UserAuthenticationPageModel : FreshBasePageModel
{
    public static UserAuthenticationPageModel Instance { get; private set; }
    public UserAuthenticationPageModel()
    {
        Instance = this;
    }

修改了 FirebaseCloudNight

中的代码
namespace iVanApp.Services { public class FirebaseCloudNight :
FirebaseCloudBaseHelper {

static public async Task<bool> SynchronizeNights()
{
    // Synchronize from Cloud to DB
    foreach (var cloudItem in cloudNights)
    {
        UserAuthenticationPageModel.Instance.CurrentProgress = currentNight / numberOfNights
        try
        { 
            //Here I removed code for retriving data from online DB
            currentNight ++;
        }
        catch (Exception ex)
        {
            return false;
        }
        return true;
    }
 }  }