【问题标题】:MVVM binding from static method静态方法的 MVVM 绑定
【发布时间】:2021-11-29 04:50:15
【问题描述】:

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

目前的情况是:

  • 我正在使用 FreshMVVM。
  • 在线获取数据表单的方法在FirebaseCloudNight类中。有一个静态方法SynchronizeNights,我整晚都用它(函数foreach)。我想根据已完成/仍需同步的夜晚显示进度条。
  • 在ViewModel 中,我拥有CurrentProgress 的财产,该财产被正确地投标给查看。我无法正确更新此属性。
  • 我遗漏了很多代码,因为我认为这些代码无关紧要。如果您需要更多代码,请告诉我。
  • 我找到的唯一解决方案是创建 MV 类的新实例。但经过进一步阅读后,我发现,虽然我看到属性发生了变化,但绑定并不好,因为它改变了新的属性实例。

问题是我发现的唯一方法是根据新实例更新此属性。但是随后 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;
        }
     }
 }

【问题讨论】:

    标签: c# xamarin xamarin.forms mvvm


    【解决方案1】:

    找到一个有效的答案

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

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 1970-01-01
      • 2016-04-02
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 2011-08-23
      • 1970-01-01
      相关资源
      最近更新 更多