Structuring Reusable Xamarin Library

Kevin Delval 186 Reputation points
2021-09-25T19:27:05.727+00:00

Over the course of a couple years of developing in Xamarin, I've made a bunch of ContentViews (such as a custom Switch) as well as a few Effects with platform-specific implementations for a number of apps. I'd like to consolidate these into a single library that I can then use in other Xamarin projects. However, I haven't been able to find very clear documentation on how to accomplish this. Obviously, all the shared code can go into a single, or multiple, shared class libraries. But what about the platform-specific renderers or effect implementations? Do I put those in a separate library? Or do I keep them in the same one but make the inclusion of dependencies such as Xamarin.Forms.Android conditional depending on what platform(s) the library is being built for?

I've noticed that Xamarin.Forms seems to detect this automatically. The Xamarin.Forms NuGet added to an Android project seems to pick up the Android dependenceis, but not the iOS ones. How is this set up?

The easy solution is to just add everything to a single library, including all the dependencies, but that seems sloppy to me. Is there anyone else that has attempted to make a library of custom views, effects, etc with platform specific code?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,231 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,146 Reputation points Microsoft Vendor
    2021-09-27T06:04:10.37+00:00

    Hello,
    Welcome to our Microsoft Q&A platform!

    You could automatically create NuGet packages to share code across platforms using the 'Nugetizer 3000', this doc shows how to create the NuGet which has two reference assemblies(both iOS and Android), you also could manually create NuGet Packages for Xamarin. For more information, you can refer to
    https://learn.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/nuget-manual
    https://learn.microsoft.com/en-us/nuget/guides/create-packages-for-xamarin

    Best Regards,
    Wenyan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful