locked
Prism DI with event attached in constructor RRS feed

  • Question

  • User384560 posted

    Hi,

    I am using the following repo in my Prism XF app to select multiple images from the gallery.

    https://github.com/CrossGeeks/MultipleMediaPickerSample

    If you check the issues you'll notice there is an issue with the code:

    https://github.com/CrossGeeks/MultipleMediaPickerSample/issues/1

    I don't really want to go down the route of using Messaging as most answers suggest but if you read this comment...

    https://github.com/CrossGeeks/MultipleMediaPickerSample/issues/1#issuecomment-563190605

    ...osamaelhosany suggests using the IOC container. I'm trying this method as follows:

    public class AndroidInitializer : IPlatformInitializer
        {
            public void RegisterTypes(IContainerRegistry containerRegistry)
            {   
                containerRegistry.RegisterSingleton<IMultiMediaPickerService, MultiMediaPickerService>();
            }
    }
    

    Then resolving in MainActivity OnActivityResult as so (I've removed SharedInstance in MultiMediaPickerService):

    var multimediaPickerService = (MultiMediaPickerService)((PrismApplication)Xamarin.Forms.Application.Current).Container.Resolve<IMultiMediaPickerService>();
    
    multimediaPickerService.OnActivityResult(requestCode, resultCode, data);
    

    In the ViewModel the interface is injected as normal and I am attaching the event to OnMediaPickedCompleted in the ViewModel constructor.

    _multiMediaPickerService.OnMediaPickedCompleted -= CreateImageBanks;
    _multiMediaPickerService.OnMediaPickedCompleted += CreateImageBanks;
    

    I am noticing that each time I navigate to the page the event is being attached again via the ViewModel constructor. That is to say the first time I enter the page and select Images from the Gallery, CreateImageBanks is fired once. The second time I enter the page and select Images from the Gallery, CreateImageBanks fires twice. The third time...and so on.

    Can anyone offer any explanation as to why?

    Thanks in advance

    Tuesday, July 28, 2020 7:53 AM

Answers

  • User384560 posted

    Gave up and used MessagingCenter

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 28, 2020 7:25 PM

All replies

  • User382871 posted

    I am noticing that each time I navigate to the page the event is being attached again via the ViewModel constructor. To avoid this issue, try to create a global static bool property to detect if the images have been loaded. For example, create a property App class in the normal xamarin.forms project.

    App.xaml.cs ``` public partial class App : Application { public static bool IsLoaded; public App() { InitializeComponent();

        MainPage = new NavigationPage(new MainPage());
    }
    

    } Page.xaml.cs public partial class MainPage : ContentPage { public MainPage() { InitializeComponent();

        if (App.IsLoaded == false)
        {
            //load the data
            App.IsLoaded = true;
        }
    }
    

    } ```

    Tuesday, July 28, 2020 11:57 AM
  • User384560 posted

    @YelinZh . I need to understand a little more about how the ViewModels work with Prism I think for that to work. The constructor is firing every time I navigate to the view. I've implemented IDisposable and noticed Dispose is not called between when I navigate away from the view then navigate to it for the second time so presumably there are 2 instances in memory.??

    Tuesday, July 28, 2020 4:07 PM
  • User384560 posted

    Gave up and used MessagingCenter

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 28, 2020 7:25 PM