0

In my application, my ValueConverter is doing some image conversion (like InMemoryImageValueConverter with extra tasks). This takes a while so my GUI freezes for half a second, which is really annoying.

As it is pretty platform specific, I can't do this in my ViewModels. So I would like to use something like an async ValueConverter to perform this operation in background. Is there a possibility?

Otherwise, what would be best practice? My alternative would be to call this code in activity, which would skip the MVVM pattern.

Matt
  • 4,612
  • 1
  • 24
  • 44
  • You can do platform specific methods also in your viewmodel .Thats not restricted to your UI. I can give you an answer if this is a way for you. But I have no idea how to implement an async ValueConverter. But I think you should not do big operations in a ValueConverter. Please provide a Little bit about what you want to do.. – Joehl Nov 05 '15 at 13:35
  • Thanks for the answer. Well. I actually have `byte[]` for an image and want to convert it to an android Bitmap. After this, I merge this bitmap on a canvas with some more bitmaps I load directly from resources. – Matt Nov 05 '15 at 13:39
  • Have you tried using a [`PriorityBinding`](https://msdn.microsoft.com/en-us/library/system.windows.data.prioritybinding%28v=vs.110%29.aspx) already? – Pieter Witvoet Nov 05 '15 at 13:42
  • The IValueConverter interface isn't `async` so I think your approach is doomed. You could implement a custom control instead (inherit from MvxView) and do the conversion in a custom property setter instead. – Stuart Nov 05 '15 at 15:18

1 Answers1

2

I found Stephen Cleary's answer to this question helpful: Async Implementation of IValueConverter

In the end I used his excellent AsyncEx library in a Windows-specific Value Converter to do async conversion of a byte array. For Android and iOS I was able to do the async conversion in MVVMCross custom bindings, without needing a Value Converter.

A little more information can be found in a blog post that I wrote: http://www.sequence.co.uk/blog/infinite-scrolling-using-mvvmcross-and-xamarin/

Community
  • 1
  • 1
Howard
  • 694
  • 5
  • 21