1

The goal is to asynchronously fetch an image from a url and then set it to a BitmapSource viewmodel property (which is databound in a view).

I can use LoadImageFromUrl to get a Splat.IBitmap, but I can't figure out how to connect that to a BitmapSource. In fact, I'm not sure that I'm using this method correctly at all.

Here's what I've got:

using Akavache;
using System.Reactive.Linq;

// ...others...

var myViewModel = ...;

var url = @"http://upload.wikimedia.org/wikipedia/meta/0/08/Wikipedia-logo-v2_1x.png";

BlobCache.LocalMachine.LoadImageFromUrl(url, false, 200, 183)
    .ObserveOnDispatcher()

    // up to here, it's fine
    // what goes below is the question

    // I manually instantiate an Action object so as to explicitly pick that override,
    // but LoadImageFromUrl returns an IObservable<Splat.IBitmap>, so I'm not picking the wrong <T> here
    .Subscribe(new Action<Splat.IBitmap>(pic => {
        // haha, nope: myViewModel.someBitmapSource = (BitmapSource)pic);
    }), ex => Debug.WriteLine(ex));

Other things I thought of:

  • Check documentation: none seems to exist that I can find
  • Google around: slim pickings, nothing directly helpful
  • Look to see how this method is used in Akavanche tests: it doesn't appear to be called anywhere
  • Since a Splat-namespace object is returned, check Splat tests/docs: Closer. The splat readme says:

.

// ToNative always converts an IBitmap into the type that the platform
// uses, such as UIBitmap on iOS or BitmapSource in WPF
ImageView.Source = ViewModel.ProfileImage.ToNative();

Great, except ToNative() doesn't make sense in this case because it's not a member of the Splat.IBitmap interface.

And now I'm out of ideas.

Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
  • `ToNative` is an [extension](https://github.com/paulcbetts/splat/blob/e923476a84d4954e4aa6804afee0186b12fb5836/Splat/Xaml/Bitmaps.cs#L138) of the `IBitmap` interface. I haven't used Akavache so maybe your question went over my head, but `ToNative` seems to be the correct way to go about it. – Chris Jun 16 '14 at 02:19
  • @Rfvgyhn ah, very good. Adding Splat as a project dependency, and then `using` it does give me that `ToNative` extension method, and then calling it in the `Subscribe` callback does work as expected. However, it's still fuzzy to me if this is the "right" way to use Akavache... – Factor Mystic Jun 16 '14 at 04:00
  • @Rfvgyhn post your comment as an answer & I'll accept it – Factor Mystic Jun 19 '14 at 01:25

1 Answers1

1

ToNative is a Splat extension of the IBitmap interface. Adding a dependency on Splat and using it in this context will make that method available.

Whether or not that's the Right™ way to do it, I'm not sure.

Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
Chris
  • 4,393
  • 1
  • 27
  • 33