Which project(s) to install shared libraries in Xamarin App - xamarin

I want to use a shared class library from my backend API in my Xamarin Forms 5 app. This library mostly contains some util functions and I have it as a NuGet package.
Do I need to install this package on all three projects or just the main project where I have my code and where I'll be using it?
I read some articles and watched some videos and got some conflicting answers. In some cases, packages are installed onto all three projects but other times only onto the main one. I'm sure there's some logic to this. I'd appreciate someone telling me how to determine where to install NuGet packages like mine?

if it's just a .NET library, install it in your .NET project where all the Forms code is
nugets that are installed in multiple projects typically contain multiple libraries - iOS, Android and .NET, and nuget installs the appropriate library in each one

Related

Install Nuget on Xamarin.Forms for every proyect or not

When you install a nuget in Xamarin.Forms is it necessary to do it on every project (project, project.Android and project.IOS)?
Which pros and cons has do it or how I know where I have to intall each of them?
There is no absolute answer, it will depend on the Nuget Package. So you will have to read the information about it before deciding to install it in your project.
What defines this?
Take for example Xamarin.Essentials, it is installed in all the projects because you can call the functions in share code, but the actual implementation is in the platform specific. In this case you even have to call Init in every platform
From Xamarin.Essentials doc
In the Solution Explorer panel, right click on the solution name and
select Manage NuGet Packages. Search for Xamarin.Essentials and
install the package into ALL projects including Android, iOS, UWP, and
.NET Standard libraries.
If you apply platform specific code, you might need some extra Nugets for that code, meaning that you would only add it to your Android or iOS or UWP code
If you have any doubt about where to install the package, you can always ask :) Also if you install an Android package in an iOS project, you surely will get a compiling error.

xamarin forms socketioclientdotnet

I'm not sure if this is a Visual Studio issue or a genuine incompatibility but trying to install SocketIoClientDotNet into a Xamarin forms PCL project I get
Could not install package 'SocketIoClientDotNet 1.0.6'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile7', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
The project targets .NetFramework 4.5 which seems to be supported by the package - what could be going wrong here?
I suggest you add the following packages in your .Droid and .iOS projects (and not in the "common" project):
EngineIoClientDotNet (v.0.9.22)
SocketIoClientDotNet (v.0.9.13)
WebSocket4Net (v.0.14.1)
These versions of packages were the stablest 6 months ago when I had to use sockets in my project. If you want more information don't hesitate to ask me!

Cannot install NuGet packages to PCL - "package does not contain any assembly references or content files that are compatible with that framework"

I am trying to follow a tutorial on consuming web services in Xamarin Forms. I created a fresh project and have tried to install Refit, that didn't work so I tried to install RestSharp. I can install both of these libraries to my Droid / iOS projects, but not to the PCL. I get the following error in Visual Studio:
Could not install package 'Refit 4.0.1'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile111', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author
Could not install package 'RestSharp 106.2.1'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile111', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author
I've found this post on the xamarin forums, but it didn't fix my issue. My project is fresh, everything is up to date... Does anyone know how I can fix this issue?
vents XF is supposed to make developing cross platform apps faster & easier, but i think I'd have done this poc in native ios/droid twice in the time it's taken me to install a library to a blank project! :)
This happens because RestSharp it's not compatible with PCL-library. So you will have to handle whatever you're trying to do in platform specific.

Cannot install Simple OData Client in Xamarin.Forms

This suggested Nuget Client will not install on Visual Studio 2017 for Mac. When I attempt to install, I receive the following error message:
"Could not install package 'Microsoft.OData.Edm 6.0.0'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile111', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author."
The link to this Nuget package is https://components.xamarin.com/view/simple.odata.client.
When trying to install other Nuget packages I receive a similar error message naming a different package as the error source.
It looks like you're mixing a couple of things. The Simple.OData.Client package is not the Microsoft.OData.Edm package. Also you say you link to a NuGet package, put you link to the Xamarin Component Store.
Furthermore, I think you should read up on what PCLs are and how to use them. When I look at the NuGet page for the Microsoft.OData.Edm package I see that it does not support iOS and Android. So, you will never be able to install it to a PCL which targets one of these platforms. You should, however, be able to install the Simple.OData.Client package according to the NuGet page.
PCLs have a certain profile. When you right-click and go to the properties of a PCL library, you can check all platforms that you want it to support. Each combination of platforms have a certain profile, specified by a three digit number, like 111 or 259. The parts of the .NET framework that are available to you, are an intersection of the functionalities that are available on all targeted platforms. If a functionality is supported by iOS but not on Android, it's not supported in your PCL.
This is also why NuGets need to support the specific profile that you are targeting, because it can only use the methods that are available in it.

C# Nuget References

I'm a bit confused about the dlls that are installed when using Nuget to install libraries to my Xamarin Projects. I mainly manage the nugets for the whole solution. So when i install a nuget i choose all platform projects as targets, meaning the PCL, the Android and the iOS Project. In most cases there are different dlls, like #.XForms, #.XForms.Android and #.Android, for example in the Community Toolkit (FormsCommunityToolkit), which is also available from Nuget.
In my understanding the .Android dlls need to be used in the android platform project, and the XForms dll without android should be used in the PCL. A good example is the Converters.dll. That's code only used in the pcl, but the dll is also automatically referenced in the platform projects. So my question is, why is this the case?
I see this behavior not only for this toolkit but also for other apis, like Syncfusion.
PCL dlls will contain codes that accessing common API for the targeted PCL profiles. For the UI project (Android and iOS) need to be using different dlls most of the time because it need to access to platform specific APIs. For example, API to check network connection is different in iOS and Android.
But you should not be worry about which dlls to use. NuGet package manager already handle which dlls to be use in the solutions. So, you will just need to include the packages and let the package manager to handle the rest of it.

Resources