Creating a custom plugin for MvvmCross involves several steps, which have been simplified significantly starting from version 6.0. Here's a detailed guide on how to create a custom plugin:
Step 1: Create a Library for the Plugin
First, you need to create a new .NET Standard library project. This library will contain the core functionality of your plugin. Ensure that it references the main MvvmCross package.Step 2: Define Interfaces and Shared Code
In this library, define any interfaces that your plugin will provide. Also, include any shared portable code that implements these interfaces. This shared code should be platform-independent.Step 3: Create a Plugin Class
Create a class that inherits from `IMvxPlugin`. This class is typically named `Plugin`. Annotate this class with the `MvxPluginAttribute`. The `Load` method in this class should handle all IoC registration and initialization for your plugin.csharp
[MvxPlugin]
public class MyPlugin : IMvxPlugin
{
public void Load()
{
// Register services here, e.g., Mvx.IoCProvider.RegisterSingleton(new MyService());
}
}
Step 4: Add Platform-Specific Implementations (Optional)
If your plugin requires platform-specific functionality, create separate assemblies for each platform (e.g., `.Droid`, `.iOS`, `.WindowsPhone`). In these assemblies, implement platform-specific interfaces or services.Step 5: Implement Platform-Specific Plugin Classes
For each platform-specific assembly, create a class that also inherits from `IMvxPlugin` and is annotated with `MvxPluginAttribute`. This class should handle platform-specific IoC registrations and initializations.csharp
// Example for Android
[MvxPlugin]
public class MyPluginAndroid : IMvxPlugin
{
public void Load()
{
// Register Android-specific services here
}
}
Step 6: Use the Plugin in Your Application
To use the plugin in your MvvmCross application, you need to install the NuGet package for the plugin in both your Core project and your platform-specific projects. If the plugin is not explicitly referenced in your code, ensure it is loaded by adding a reference somewhere in your project.Step 7: Testing and Deployment
Ensure that your plugin works as expected by testing it thoroughly. For distribution, package your plugin as a NuGet package to make it easily installable in other projects.Additional Notes
- Bootstrap Files: In versions prior to MvvmCross 6.0, you might need to manually create bootstrap files to initialize plugins. However, this is no longer necessary in newer versions.- Plugin Manager: The `IMvxPluginManager` is responsible for loading plugins. You can use its methods to reload plugins during testing.
- Plugin Structure: Keep your plugin structure organized with clear naming conventions to ensure easy reuse across projects.
By following these steps, you can create a custom plugin for MvvmCross that is both reusable and easily maintainable across different platforms.
Citations:[1] https://stackoverflow.com/questions/16788164/custom-plugin-in-mvvmcross
[2] https://www.mvvmcross.com/documentation/plugins/plugins-development
[3] https://www.mvvmcross.com/documentation/fundamentals/inversion-of-control-ioc
[4] https://www.mvvmcross.com/documentation/plugins/getting-started
[5] https://github.com/MvvmCross/MvvmCross-Samples
[6] https://github.com/MvvmCross/MvvmCross/issues/872
[7] https://pmichaels.net/2015/01/25/mvvm-cross-creating-a-new-plug-in/
[8] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[9] https://www.mvvmcross.com/documentation/getting-started/getting-started
[10] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup
[11] https://www.nuget.org/packages/MvvmCross.Plugin.Visibility/