我嘗試以 xamarin 形式開發(fā)移動(dòng)應(yīng)用程序。我有新聞列表,我正在使用 SQLite 來(lái)存儲(chǔ)它。當(dāng)我的主頁(yè)打開時(shí),在我從 SQLite 獲取新聞之前,然后我嘗試從 Web API 獲取新新聞。我可以在 SQLite 中保存新新聞,但我的 UI 沒(méi)有改變。我該如何解決這個(gè)問(wèn)題? public MainPage() { InitializeComponent(); database = new Helper.xxxDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "xxx.db3")); SetLocalDBMainNews(); Task.Run(async () => { await GetNewsAsync(); }); } void SetLocalDBMainNews() { if(database.TableExists("News")) { List<News> newsList = database.GetNewsData(); if (newsList.Count > 0) { MaiNewsList.ItemsSource = newsList.Where(x => x.Type.Equals((int)NewsType.MainNews)).OrderBy(x => x.Sort).ToList(); } } else database.CreateNewsTable(); } private async Task GetNewsAsync() { try { var register = database.GetRegisterData(); string url = "http://xxx.azurewebsites.net/api/News/GetPublishedNews"; var response = Tools.Send(url, "GET", register.Token); var result = JsonConvert.DeserializeObject<ApiResponse<List<News>>>(response); if (result.HasError == false) { var newsList = result.Value; database.DeleteAllNews(); foreach(var n in newsList) { database.Insert(n); } MaiNewsList.ItemsSource = newsList.Where(x => x.Type.Equals((int)NewsType.MainNews)).OrderBy(x => x.Sort).ToList(); } } catch{} }該代碼適用于將新新聞保存到 SQLite,但我無(wú)法將新聞異步綁定到 UI。
如何在xamarin表單中使用異步更改更改ui
飲歌長(zhǎng)嘯
2023-08-13 16:12:02