To Use the Libraries in an Application:
- Create a “/lib/” directory in each project
- Copy the XamUtils.Abstractions.dll library into the /lib directory
- For PCL projects, copy the XamUtils.dll PCL library (the PCL stubs library) into the /lib directory
- For platform-specific projects, copy the platform-specific XamUtils.dll library for that platform into the /lib directory
- Keep the DLLs up-to-date in each project by your continuous integration tool
- Add references to the “XamUtils.Abstractions” project and the “XamUtils” project
Additional Considerations:
- For classes where the implementation is just completely different between the different platforms or where the parameters or return values from the methods cannot be easily abstracted into common types, the simplest solution is to create custom methods for that platform while keeping the method signatures as similar as possible
- Even though the methods are entirely different, methods serving a similar purpose should still be grouped into a class of the same name.
- For example, image handling varies differently enough between iOS and Android that your code might look like this:
- On iOS:
public class ImageUtils { public static UIImage MaxResizeImage( UIImage sourceImage, float maxWidth, float maxHeight) { .. } public static UIImage CropImage( UIImage sourceImage, int left, int top, int width, int height) { .. } }
- On Android:
public class ImageUtils { public static Bitmap MaxResizeImage( Bitmap sourceImage, float maxWidth, float maxHeight) { .. } public static Bitmap CropImage( Bitmap bitmap, int left, int top, int width, int height) { .. } }
- On iOS:
Conclusion
Although all the above may look complicated, it really is fairly straightforward once you’ve run through it once or twice. Following this pattern eliminates much of the indirection and “noise” that the normal methods of accessing cross-platform code in Xamarin usually requires, freeing up the developer to focus entirely on coding the main application.