来自静态方法的 MVVM 绑定
MVVM binding from static method
我正在开发我的应用程序,它将从在线数据库中获取数据。我想显示进度条。问题是我无法让它工作。
目前情况是:
- 我正在使用 FreshMVVM。
- 在线获取数据的方法在class
FirebaseCloudNight
中。有一个静态方法SynchronizeNights
,我整晚都用它(功能foreach
)。我想根据要同步的夜晚 done/remaining 显示进度条。
- 在
ViewModel
中,我有 属性 CurrentProgress
已正确出价查看。我无法正确更新此 属性。
- 我遗漏了很多代码,因为我认为这些代码无关紧要。如果您需要更多代码,请告诉我。
- 我找到的唯一解决方案是创建 MV class 的新实例。但是在进一步阅读之后我发现,虽然我看到 属性 发生了变化,但绑定并不成功,因为它改变了 属性.
的新实例
问题是我发现的唯一方法是根据新实例更新此 属性。但是 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;
}
} }
我正在开发我的应用程序,它将从在线数据库中获取数据。我想显示进度条。问题是我无法让它工作。
目前情况是:
- 我正在使用 FreshMVVM。
- 在线获取数据的方法在class
FirebaseCloudNight
中。有一个静态方法SynchronizeNights
,我整晚都用它(功能foreach
)。我想根据要同步的夜晚 done/remaining 显示进度条。 - 在
ViewModel
中,我有 属性CurrentProgress
已正确出价查看。我无法正确更新此 属性。 - 我遗漏了很多代码,因为我认为这些代码无关紧要。如果您需要更多代码,请告诉我。
- 我找到的唯一解决方案是创建 MV class 的新实例。但是在进一步阅读之后我发现,虽然我看到 属性 发生了变化,但绑定并不成功,因为它改变了 属性. 的新实例
问题是我发现的唯一方法是根据新实例更新此 属性。但是 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; } } }