感谢在过去几个月里帮助我帮助我启动并运行我的silverlight / f#原型的所有人(在RC版本的VS-Ugh中启动)。我们试图解决的最后一个问题是RPC问题。
我们需要能够对RPC调用进行分页,以便请求第一页并将其绑定到网格并显示,同时在后台预填充otehr页并连接在一起。我猜psuedo代码看起来像这样:
let pageNo = 1
let page1Data = JsonRpc.getSomeData(pageNo)
let grid.datasource <- page1Data
let grid.suspendFiltering <- true
// run the remainder in background
let allData : list ref = page1Data ref
for pageNo in [2..totalPages]
allData := allData @ JsonRpc.getSomeData(pageNo)
let grid.datasource <- allData
let grid.suspendFiltering <- true
我为上面的代码道歉,我尽量把它作为F#(在这个文本窗口中写);另一个缺陷是需要使用回调将数据绑定到网格等。
问题方法可能会被用来解决这个问题,什么是最合适的?
答案 0 :(得分:1)
module Loader
open System
open System.Threading
let totalPages = 20
// emulation of long-running data loading routine
let private loadPageData (page : int) =
async {
do! Async.Sleep(1000)
return List.replicate 5 page
}
// loader - notifies UI about new data via callback
let loadAsync (callback : System.Action<_>) =
let syncContext = SynchronizationContext.Current
let doLoad = async {
// load first page and immediately feed it to callback
let! page1Data = loadPageData 1
do! Async.SwitchToContext syncContext
callback.Invoke(ResizeArray<_>(page1Data))
// load remaining data in the background
do! Async.SwitchToThreadPool()
let allData = ResizeArray<_>(page1Data)
for page in 2..totalPages do
let! pageData = loadPageData page
allData.AddRange(pageData)
do! Async.SwitchToContext syncContext
callback.Invoke(allData)
}
Async.Start doLoad
在UI方面,它将显示为这样(即数据 - ListBox或其他一些控件)
Loader.loadDataAsync(list => data.ItemSource = list)