question

KevinDelval avatar image
0 Votes"
KevinDelval asked KevinDelval commented

Structuring Reusable Xamarin Library

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?

dotnet-csharpdotnet-xamarinformsdotnet-androiddotnet-ios
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered KevinDelval commented

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://docs.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/nuget-manual
https://docs.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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is exactly what I needed; thank you very much.

0 Votes 0 ·