Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue choosing image with mvvmcross and mono

Iam using the following two methods to choose a image in the users imagefolder. However my issue is, after every time the ChoosePhotoForEventItem() is called, it seems that service remembers the supplied action(UploadImage). This means that the second time ChoosePhotoForEventItem() is called, the method UploadImage is triggered two times and the third time it is triggered three times. I tried to convert the method to an property and set it to null, by that do not change a thing.

       public void ChoosePhotoForEventItem()
    {
        var picChooser = this.GetService<IMvxPictureChooserTask>();

        picChooser.ChoosePictureFromLibrary(MaxPixelDimension, DefaultJpegQuality,this.UploadImage,delegate {/*Do nothing on cancel*/});

    }

    private void UploadImage(Stream stream)
    {
        this.UploadImage(stream, ItemID);
    }

Any help is highly appreciated

like image 359
Bjarke Avatar asked Dec 29 '25 18:12

Bjarke


2 Answers

Looking at the plugin, the task is registered for a new instance for every call to GetService

        this.RegisterServiceType<IMvxPictureChooserTask, MvxImagePickerTask>();

in https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Plugins/PictureChooser/Cirrious.MvvmCross.Plugins.PictureChooser.Touch/Plugin.cs

It also looks like each MvxImagePickerTask instance creates it's own UIImagePickerController in https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Plugins/PictureChooser/Cirrious.MvvmCross.Plugins.PictureChooser.Touch/MvxImagePickerTask.cs

So I can't spot anything wrong in the plugin, especially as your code is calling var picChooser = this.GetService<IMvxPictureChooserTask>(); each time.


Do you have a simple sample app at all that people can try running to debug this?

If not, can you try running something like:

private int _counter = 0;

public void ChoosePhotoForEventItem()
{
     _counter++;
     var localCounter =  _counter;
    var picChooser = this.GetService<IMvxPictureChooserTask>();

    picChooser.ChoosePictureFromLibrary(MaxPixelDimension, 
         DefaultJpegQuality,
         (stream) => this.SpecialUploadImage(stream, localCounter),
          delegate {/*Do nothing on cancel*/});
}

private void SpecialUploadImage(Stream stream, int theCounter)
{
    MvxTrace.Trace("Callback for localCounter {0}", theCounter);
    this.UploadImage(stream, ItemID);
}

The trace from that would help us work out what action is being called three times

like image 73
Stuart Avatar answered Jan 04 '26 22:01

Stuart


The issue is, as Stuart points out, that the service I am using is a singleton and it is not instantiated every time I am using the service. Therefore for now, I am I am doing a quick fix with a simple boolean to avoid any unwanted upload. Stuart informed me the version of mvvm I am using is quite old. So it seems that the switching to vNext in the nearest future is the only way forward.

like image 37
Bjarke Avatar answered Jan 04 '26 23:01

Bjarke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!