我已經閱讀了幾篇文章,但我很難理解這一點。我試圖在加載一些資料時保持我的 MAUI 應用程式的 UI 更新。
我想完成這個:
async public DataService()
{
//initialize stuff here
await this.GetPayees();
await this.GetCategories();
return;
}
我讀到你不能有一個異步建構式,所以我不得不重做我如何初始化我的類。
public DataService()
{
//Take this out here
//this.GetPayees();
//this.GetCategories();
return;
}
async public static Task<DataService> BuildDataServiceAsync()
{
//await them here
var dataService = new DataService();
await dataService.GetPayees();
await dataService.GetCategories();
return dataService;
}
這在我的代碼中產生了級聯效應。我必須將回傳型別更改為任務,并使其他方法異步
async public Task<List<Payee>> GetPayees()
{
//Load arbitrary data,
if(Payees.Count != 0) return Payees;
Payees.Add(new Payee { Id = 0, Name = "Food Lion", DefaultCategoryId = 0, DefaultIsCredit = false });
Payees.Add(new Payee { Id = 1, Name = "Work Incorporated", DefaultCategoryId = 1, DefaultIsCredit = true });
Payees.Add(new Payee { Id = 2, Name = "Hardees", DefaultCategoryId = 3, DefaultIsCredit = false });
Payees.Add(new Payee { Id = 3, Name = "Wal-Mart", DefaultCategoryId = 5, DefaultIsCredit = false });
Payees.Add(new Payee { Id = 4, Name = "Aldis", DefaultCategoryId = 0, DefaultIsCredit = false });
Payees.Add(new Payee { Id = 5, Name = "McDonalds", DefaultCategoryId = 3, DefaultIsCredit = false });
Payees.Add(new Payee { Id = 6, Name = "Harris Teeter", DefaultCategoryId = 0, DefaultIsCredit = false });
Payees.Add(new Payee { Id = 7, Name = "Shoe Show", DefaultCategoryId = 2, DefaultIsCredit = false });
Payees.Add(new Payee { Id = 8, Name = "Capital One", DefaultCategoryId = 4, DefaultIsCredit = false });
Payees.Add(new Payee { Id = 9, Name = "Dicks Sporting Goods", DefaultCategoryId = 6, DefaultIsCredit = false });
Payees.Add(new Payee { Id = 10, Name = "Amazon", DefaultCategoryId = 7, DefaultIsCredit = false });
return Payees;
}
async public Task<List<Category>> GetCategories()
{
if(Categories.Count != 0) return Categories;
Categories.Add(new Category { Id = 0, Name = "Groceries" });
Categories.Add(new Category { Id = 1, Name = "Paycheck" });
Categories.Add(new Category { Id = 2, Name = "Shoes" });
Categories.Add(new Category { Id = 3, Name = "Fast Food" });
Categories.Add(new Category { Id = 4, Name = "Credit Card" });
Categories.Add(new Category { Id = 5, Name = "Supplies" });
Categories.Add(new Category { Id = 6, Name = "Recreation" });
Categories.Add(new Category { Id = 7, Name = "Grocery" });
Categories.Add(new Category { Id = 8, Name = "Gross" });
Categories.Add(new Category { Id = 9, Name = "Grass" });
return Categories;
}
我收到關于在最后兩種方法中沒有等待的編譯器警告。我真的會等待所有的 Add() 嗎?
我只是讓依賴注入按照我想要的方式作業,但這一切都級聯回我的視圖模型,它注入了我的依賴項:
public PayeesViewModel(DataService dataService, NavigationService navigationService, ValidationService validationService)
{
this.dataService = dataService;
this.navigationService = navigationService;
this.validationService = validationService;
Payees = await dataService.GetPayees();
Categories = await dataService.GetCategories();
for(int x = 0; x < Payees.Count; x )
{
PayeeDisplay.Add(new PayeeDisplay
{
Id = Payees[x].Id,
Name = Payees[x].Name,
DefaultCategory = Categories.Find(c => c.Id.Equals(Payees[x].DefaultCategoryId)).Name,
DefaultCategoryId = Payees[x].DefaultCategoryId,
DefaultIsCredit = Payees[x].DefaultIsCredit
});
}
}
我會以錯誤的方式解決這個問題嗎?嘗試采用 async/await 簡直就是在毀掉我的專案。
uj5u.com熱心網友回復:
該async關鍵字僅允許使用await. 如果您不需要使用await,那么您就不需要async.
List.Add()不是異步方法,因此您不會使用await. 所以只需洗掉async關鍵字;你不需要它。
當 CPU 必須等待外部資源時,異步編程很有用,例如:網路請求、檔案存盤請求等——任何不在 CPU 或 RAM 中的資源。將物件添加到集合中純粹是記憶體操作,因此不適合異步編程。
我認為閱讀 Microsoft 關于使用 async 和 await 進行異步編程的檔案會對您有所幫助。它寫得很好。
更新:
既然你說對的呼叫Add()只是 SQL 訪問的占位符,那么是的,你會想要async用于 SQL 呼叫,而且你這樣做的方式是正確的:創建一個public static async將為你創建類實體的方法。這稱為“工廠方法”。您甚至可以更進一步,將建構式設為私有,這樣您就可以強制使用工廠方法:
private DataService() {}
public static async Task<DataService> BuildDataServiceAsync()
{
var dataService = new DataService();
await dataService.GetPayees();
await dataService.GetCategories();
return dataService;
}
GetPayees()如果物件處于可用狀態且GetCategories()必須呼叫它,那么擁有私有建構式很重要。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/537068.html
標籤:C#异步异步等待
上一篇:函式無法從XHTML請求中呼叫值導致另一個函式,記錄未定義(tampermonkey)
下一篇:異步搜索欄
