我正在開發我的應用程式,它將從在線資料庫中獲取資料。我想顯示進度條。問題是我不能讓它作業。
目前的情況是:
- 我正在使用 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; } } }
視圖模型代碼
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; } } }
uj5u.com熱心網友回復:
找到了一個有效的答案
在后面的 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; } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313148.html
標籤:C# 沙马林 xamarin.forms 虚拟机
