Announcing FreshMvvm 2.0

Today we’ve published up to nuget FreshMvvm 2.0 with some very anticipated new features.

IOC Container Lifetime Registration Options

We now support a fluent API for setting the object lifetime of object inside the IOC Container.

// By default we register concrete types as 
// multi-instance, and interfaces as singletons
FreshIOC.Container.Register<MyConcreteType>(); // Multi-instance
FreshIOC.Container.Register<IMyInterface, MyConcreteType>(); // Singleton 

// Fluent API allows us to change that behaviour
FreshIOC.Container.Register<MyConcreteType>().AsSingleton(); // Singleton
FreshIOC.Container.Register<IMyInterface, MyConcreteType>().AsMultiInstance(); // Multi-instance

As you can see below the IFreshIOC interface methods return the IRegisterOptions interface.

public interface IFreshIOC
{
    object Resolve(Type resolveType);
    IRegisterOptions Register<RegisterType>(RegisterType instance) where RegisterType : class;
    IRegisterOptions Register<RegisterType>(RegisterType instance, string name) where RegisterType : class;
    ResolveType Resolve<ResolveType>() where ResolveType : class;
    ResolveType Resolve<ResolveType>(string name) where ResolveType : class;
    IRegisterOptions Register<RegisterType, RegisterImplementation> ()
        where RegisterType : class
        where RegisterImplementation : class, RegisterType;
}

The interface that’s returned from the register methods is IRegisterOptions.

public interface IRegisterOptions
{
    IRegisterOptions AsSingleton();
    IRegisterOptions AsMultiInstance();
    IRegisterOptions WithWeakReference();
    IRegisterOptions WithStrongReference();
    IRegisterOptions UsingConstructor<RegisterType>(Expression<Func<RegisterType>> constructor);
}

Switching out NavigationStacks on the Xamarin.Forms MainPage

There’s some cases in Xamarin.Forms you might want to run multiple navigation stacks. A good example of this is when you have a navigation stack for the authentication and a stack for the primary area of your application.

To begin with we can setup some names for our navigation containers.

public class NavigationContainerNames
{
    public const string AuthenticationContainer = "AuthenticationContainer";
    public const string MainContainer = "MainContainer";
}

Then we can create our two navigation containers and assign to the MainPage.

var loginPage = FreshMvvm.FreshPageModelResolver.ResolvePageModel<LoginViewModel>();
var loginContainer = new FreshNavigationContainer(loginPage, NavigationContainerNames.AuthenticationContainer);

var myPitchListViewContainer = new MainTabbedPage(NavigationContainerNames.MainContainer);

MainPage = loginContainer;

Once we’ve set this up we can now switch out our navigation containers.

CoreMethods.SwitchOutRootNavigation(NavigationContainerNames.MainContainer);

New public methods

CurrentNavigationServiceName and PreviousNavigationServiceName are both public methods, so you can access them if you need to.

Some features (recently released)

Multiple Navigation Services
Custom IOC Containers
WhenAny
Pushing different views
Clean up after page is Popped

Summary

As always this is available on nuget now!

If you have any questions please ask on forums.xamarin.com and send me a email.

You can also find some more docs on the github repo.

Thanks

5 Responses

  1. I’m unsure what happens when you create MainTabbedPage and pass it the containerName, how is the navigation container for ‘main container’ created?

    1. var myPitchListViewContainer = new MainTabbedPage(NavigationContainerNames.MainContainer);

      This line does all the setting up for the MainContainer as a tabbed page, it put itself into the container so you can call it with the name NavigationContainerNames.MainContainer.

  2. Hi Michael,

    In the above code the class `MainTabbedPage`, is it from freshmvvm2.0 or user defined class to set up tab pages ?

    1. MainTabbedPage is a page that’s inherited from FreshTabbedContainer, we do this so that we can override functionality.

Leave a Reply