3

We're using Prism.DryIoc.Forms to create apps with Xamarin.Forms. To minimize the startup time of the app we are using the Lazy<> pattern for classes with a lot of dependencies.

This used to work fine with Prism.Unity.Forms. However, I can't get it to work with Prism.DryIoc.Forms. Any help would be appreciated.

The code is as follows. We have a page view model like this:

public class MySamplePageViewModel
{
    private readonly Lazy<ISomeClass> _lazySomeClass;

    public MySamplePageViewModel(Lazy<ISomeClass> lazySomeClass)
    {
        _lazySomeClass = lazySomeClass;
    }

    public void SomeMethod()
    {
        _lazySomeClass.Value.DoIt(); //throws exception
    }
}

However, after the page view model is being instantiated, when calling _lazySomeClass.Value we get an exception with message "Container is no longer available (has been garbage-collected).". It seems to be related to how Prism resolves the view model, because when calling the following it works fine:

var container = (Application.Current as PrismApplicationBase<IContainer>).Container;
var lazySomeClass = container.Resolve<Lazy<ISomeClass>>();
lazySomeClass.Value.DoIt(); //works fine

we're doing the registration like this:

container.Register<ISomeClass, SomeClass>(Reuse.Singleton);
container.RegisterTypeForNavigation<MySamplePage, MySamplePageViewModel>("MySamplePage");
Jacco Dieleman
  • 1,316
  • 14
  • 14
  • What is the version of DryIoc itself? – dadhi Sep 14 '17 at 04:40
  • I'm using DryIoc 2.10.1 because that is the only one I could get to work with Prism.DryIoc.Forms 6.3.0.1. With higher versions of DryIoc I get `Could not load file or assembly 'DryIoc, Version=2.12.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.` when the container is being accessed. – Jacco Dieleman Sep 14 '17 at 06:51

1 Answers1

3

The problem should be fixed in v2.10.3.

Therefore the next logical step is to ask Prism.DryIoc.Forms maintainers to update to the latest DryIoc version.

dadhi
  • 4,807
  • 19
  • 25
  • 1
    Prism 7 already targets a newer version. If you use the latest 6.3 release then you simply need to update DryIoc in your application. – Dan Siegel Sep 14 '17 at 15:04
  • I tested Prism.DryIoc.Forms version 7.0.0.51-ci which uses DryIoc version 2.10.7 and it worked like a charm. @DanS. simply updating DryIoc without updating prism didn't work. It seems that Prism.DryIoc.Forms 6.3.0.1 only works with DryIoc 2.10.1 (and not a higher version) – Jacco Dieleman Sep 20 '17 at 08:24