Search code examples
xamarin.formsffimageloading

Calling GetImageAsPngAsync of FFImageLoading from ViewModel class?


Using https://github.com/luberda-molinet/FFImageLoading for Xamarin Forms

My XAML:

 <forms:CachedImage
CacheType="All"
RetryDelay="1000"
DownsampleHeight="150"
RetryCount="5"
CacheDuration="1"
BackgroundColor="WhiteSmoke"
BitmapOptimizations="True"
SuccessCommand="{Binding CcCommand}"
Source = "{Binding Photo1}">

My ViewModel Class:

class AboutMeViewModel : INotifyPropertyChanged
{
    public ICommand CcCommand { get; set; }

    public AboutMeViewModel()
    {
        CcCommand = new Command<CachedImageEvents.SuccessEventArgs>(ffFinishLoading);
    }

    void ffFinishLoading(CachedImageEvents.SuccessEventArgs ea)
    {
       // Here I would like to call 'GetImageAsPngAsync'
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

At ffFinishLoading I would like to call GetImageAsPngAsync but I can't find a way to do it at ViewModel class as I have no option for reference forms:CachedImage from the XAML file to ViewModel file.

Any help will be much appreciated.


Solution

  • var stream = await ImageService.Instance.LoadUrl(Photo1).AsPNGStreamAsync();