janp
janp

Reputation: 31

Define a base class for application class in Xamarin.Android and MvvmCross

I have an XYZ module, which is mainly a pre-built Xamarin Android java bindings library with some c# classes. In a "standalone" Xamarin Android project it works perfectly. But now I would like to implement as a plugin in my big cross-platform Xamarin project, which uses the MvvmCross framework, and follows the MvvmCross standard patterns.

The XYZ module requires to set a base class for the Android Application class, like this:

[Application]
public class Application : XYZApplication
{
  public Application(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
  { }
  public override void OnCreate()
  { base.OnCreate();}
}

Normally no application class is presented, the MvvmCross Setup.cs gets it from the core project, the CreateApp() method returns this class from the core:

public class App : MvxApplication
{
   public override void Initialize()
   {
     CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
     RegisterAppStart<ViewModels.StartupViewModel>();
    }
}

If I don't specify the Application class with the ApplicationAttribute, the XYZ module fails in its plugin. If I do, then the MvvmCross framework fails, when it want to load my plugins, the pluginloader fails with the very first plugin at this line:

manager.EnsurePlatformAdaptionLoaded<PluginLoader>(); 

with the message "could not resolve type".

I think the source of the problem, that the Application class cannot have two base classes, or cannot be created twice? Maybe there is a solution, if you need an application class defined, having a base class while using MvvmCross.

Upvotes: 2

Views: 2790

Answers (1)

Stuart
Stuart

Reputation: 66882

The MvvmCross App is a PCL class which provides a place to initialize and list all your ViewModels and other Model and Service code.

It's completely independent of any OS-specific Application or AppDelegate objects.

If you need a platform-specific Application then add one as a separate class.

We've tried to explain this a bit in https://github.com/MvvmCross/MvvmCross/wiki/High-Level-MvvmCross-Objects

See also MvvmCross: Android Application attribute

Upvotes: 3

Related Questions