I'm new to Xamarin, and am trying to build a simple Android app with F#. I'm trying to load in data from a REST API with async, and then display it. I understand that interacting with the UI must be done on the MainThread, and that there is something along the lines of Activity.RunOnUiThread()
. I've tried the following:
let onSearch args =
let search = this.FindViewById<EditText>(Resource_Id.search)
let searchResults = this.FindViewById<TextView>(Resource_Id.searchResults)
button.Text <- search.Text
async {
let! results = recipeSearch.GetRecipes search.Text
searchResults.Text <- results
}
|> Async.Start
button.Click.Add onSearch
Which throws the Exception about interacting with the UI elements in another thread. And this:
let result = async {
let! results = recipeSearch.GetRecipes search.Text
return results
}
|> Async.RunSynchronously
searchResults.Text <- result
Defeats the purpose of doing it Async
Thanks
Try this:
let onSearch args =
let search = this.FindViewById<EditText>(Resource_Id.search)
let searchResults = this.FindViewById<TextView>(Resource_Id.searchResults)
button.Text <- search.Text
async {
let! results = recipeSearch.GetRecipes search.Text
this.RunOnUiThread(fun () -> searchResults.Text <- results)
}
|> Async.Start
button.Click.Add onSearch
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.