0

I would like to change the following code to be Observable-based:

// '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)
    };
}

But when I try to change it, I end up with some design problems:

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);

I feel I'm going about this the wrong way...

Maximus
  • 1,441
  • 14
  • 38

1 Answers1

0

In the end I needed to change my logic due to another limitation with my Image control, but my solution was in the following direction using Zip:

Observable
    .Zip(
        assets
            .ToObservable()
            .Do(file => imageControl.Source = new BitmapImage(new Uri(file.Path))),
        imageControl
            .Events()   // Extension class for my events
            .ImageOpened,
        (asset, _) =>
        {
            // Do some work ...
        })
    .Subscribe(
        _ => { },
        ex => System.Diagnostics.Debug.WriteLine("Error on subscribing to Zip"));
    .DisposeWith(_subscriptions);
Maximus
  • 1,441
  • 14
  • 38