Add reference a class library to a portable class library xamarin - xamarin

i have a problem that i want to add an ordinary class library to a portable class library in Xamarin. The VS2015 community says that i can only add reference portable class library to another portable class library. So i want to ask if there is a possibility that i can add ordinary to portable library or convert it to PCL. Thanks in avance.
explanation in VS2015 community

Related

Create a Xamarin.Forms library from a Xamarin.Android / Xamarin.IOS library

Maybe this is a very basic question but here it is :
I have 2 libraries :
1 Xamarin.Android library
1 Xamarin.iOS library
What's the simplest way to create a Xamarin.Forms library that wraps the native libraries and will call them when needed?
I assume you mean actual platform libraries and not apps.
If you do mean the apps:
You're thinking of the projects backwards. The X.Forms library doesn't reference or call the platform libraries (X.Android & X.iOS); the platform libraries call the Xamarin.Form library. Just make sure both platform implementations implements the same interface and code against that interface in the Xamarin.Forms project.
If you're creating something like a plugin or control to use in Xamarin.Forms then you need to jump through a few hoops. I would suggest following James Montemagno's pattern(s). (He's a PM for Xamarin that makes a ton of nugets)
Use the old Visual Studio Extension that will stub out the projects for you. Unfortunately it uses PCLs.
Use the pattern he uses for Xamarin.Essentials. It uses fancy build conditions and only has one project.
If you want to hand roll a reusable control/nuget it yourself, you will need an abstraction to code against in your Xamarin.Forms library and some form of Dependency Injection to get to the native implementation.
Here is a good blog post about PCL bait and switch (which is what happens with the nugets)
tl;dr
You will need dependency injection. If aren't already using it, use the DependencyService that comes with Xamarin.Forms.
Assuming both the libraries do the same thing: you can use the strategy pattern to expose those libraries to X.Forms:
1.) In your X.Forms project, create a common Interface that exposes the methods you want to call in the libraries:
public interface ICommonService
{
void DoSomething();
void DoSomethingElse();
}
2.) In both of your platform projects, provide a concrete implementation of this interface:
public class CommonService : ICommonService
{
public void DoSomething(){...}
public void DoSomethingElse(){...}
}
Because the concrete implementation are in your platform projects, they have access to the Xamarin.Android and Xamarin.iOS libraries you want to leverage.
3.) Use dependency injection to create an instance of the service in your X.Forms project. One way is to use the DependencyService in your X.Forms project:
DependencyService.Get<ICommonService>().DoSomething();
Depending on if you are running the iOS or Android app, this will call the provided concrete implementation.
Note: If you plan on using DependencyService, you will need to register your concrete implementations first.
The following documentation has a very nice walkthrough of how to use the Xamarin.Forms DependencyService to achieve the strategy pattern:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

How to use Localization in Shared Project? Xamarin.Forms

I want to make a multilanguage program via using resources(.resw files).
Its really easy for PCL but I dont know how to do it in Shared Project?
Create a Portable Class Library (PCL, or just Class Library Project) using .NET Standard, in order to localize resources in a Xamarin.Forms shared project.
Create the PCL, and then reference it from all 3 projects (Android, iOS, UWP).
Using a naming convention like AppResource.resx for the main resource, select that code generation should be Public, from inside the resource editor (in the top toolbar, there is a drop-down.)
Afterward, create a resource filed named AppResources.fr-FR.resx for French, for example. Always use the format ResourceFile.Language.resx.
Code generation will automatically be disabled for the localized resource when you name it, by the project manager. Keep it that way. It doesn't need code generation.
VoilĂ ! You can now access localized resources from a shared Xamarin.Forms app using a Portable Class Library.
Now you can follow the rest of This Tutorial From Microsoft from within the PCL.
I've faced some day ago the same problem (and with .net standard there isn't documentation about it).
I've created a library to do quickly the localization also in shared project.
Hope it helps:
https://github.com/andreabbondanza/DewXamarinLocalization

Xamarin Shared Library and PCL

What is the exact difference between xamarin shared project and portable class library?
When to use shared library and when to use portable class library?
Is this possible to write native functionality in shared projects like showing alert,accessing camera and use it for both android and iOS?
Can anyone please explain me.
In shared projects each code file will be compiled for each destination (Android, iOS, Windows Phone etc). You are able to include platform specific code by using #if compiler directives.
When you want to access the camera you need to write the access code inside an #if block for all destinated platforms. This can mess up your code but it can be easier to find the different implementations.
Learn more: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/shared_projects/
Protable Class Libraries (PCL) are compiled against a general .NET subset which is compatible to all platforms you want. So you can access System.Net.Http but you cannot access any platform specific code. If you want to access the camera inside the PCL code then you need to access it by a generalized interface via dependency injection. There are some pretty good frameworks helping you to archieve this goal. One of the most famous is MVVMCross (https://github.com/MvvmCross/MvvmCross/wiki). Learn more about PCL: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/#Portable_Class_Libraries
I personally perefer PCLs because the code is much easier to read without any compiler directives. Using MVVMCross you are able to use plenty of plugins via NuGet. So you don't need to write your own classes for camera access, showing alerts etc.

Adding a dll to a Website adds a Load of Other dll's?

In Visual Studio 2010 have a website project, a class library projects, and a console app.
The class library project talks to YouTube and references dll's Google.GData.Client, Google.GData.Extensions, Google.GData.YouTube etc..
When I add a reference to this project from the console app it just adds the class library dll.
But when I add a reference to the class library project from the website it automatically adds all the google dlls.
Why is this? The console app behaves as I would expect just adding the reference to the class library, but the website adds all the dlls that the class library is dependent on also to the website.
Really I don't want this because I don't want the website to have any knowledge of the underlying framework (youtube). e.g. I don't want developers to be able to create youtube video objects. I have a wrapper class for this so if the underlying video repository changes I won't have to make changes all over the website.
All directly or indirectly dependent assemblies will need to be present in order for your Web application to use your class library. Your console application, when built, will also have the dependencies in the same folder as the executable. If you do not want it readily apparent that you use the Google libraries, you will need to embed them into your class library's assembly.
Another SO question on the topic of dependencies is here.

how do you execute silverlight class library?

When execute the silverlight class library,debugging(F5) is disable.how do I test whether my class library is right?Silverlight class library is only reusable class and components from other project?
how can share the code between the different class libraries by linking source code files between them in silverlight application??
you need to write a demo or test project to exercise the code in your library. typically, from within the project you want to use your library in, you add a reference to the assembly that contains the classes that you want to use. now, for silverlight, you are only allowed to use libraries/assemblies that are silverlight libraries--you are not allowed to use the "normal" windows class libraries.

Resources