我想将以下代码更改为基于 Observable:
// 'assets' is a IReadOnly list of StorageFile (approx. 10-20 files)
foreach (var file in assets)
{
img.Source = new BitmapImage(new Uri(file.Path));
img.ImageOpened += async (sender, e) =>
{
// Do some work (can contain Task-based code)
};
}
但是当我尝试改变它时,我最终遇到了一些设计问题:
assets
.ToObservable()
.Select(file =>
{
img.Source = new BitmapImage(new Uri(file.Path));
return img.Events().ImageOpened;
})
.Switch()
.Select(event =>
{
// Now I'm stuck, I don't have the file...
})
.Subscribe(
_ =>
{
},
ex => System.Diagnostics.Debug.WriteLine("Error on subscribing to ImageOpened"))
.DisposeWith(_subscriptions);
我觉得我走错了路……