Extracting common code from Xamarin Android project - xamarin

I've been writing a Xamarin Android app and am now in a position where I have a load of generic business logic (including SQLite.Net stuff). I would like to move this to a separate project, in case I decide to have a go at a Xamarin iOS project. Can this just be a vanilla class library project or is there more to it?
This business logic code contains some strings that will need localizing. I guess the only option will be to store them in a resx resource file in the new project?

For the project itself you can use a PCL, this is in fact my favorite option of sharing code between projects in a Xamarin solution or you can go with Shared Projects which I don't like it very much but many people use it and it works. Here's the link I always use as reference when asked which one to use so you can decide yourself based on the pros and cons of each one.
About the localization you can definitely use a resx resource file but you will need to implement your own localization service to read from the files.

Related

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

Creating apps using the same code with visual studio

I am getting in to developing multi platform apps with xamarin but need to build one main project with lots of other similar apps using some of the same code and screens. Is there a way to share code between projects or add different targets like in Xcode?
1) If you want to share code between a few apps, you can add "Portable Class Library" to your project. That way allows you to write 100% compatible code across any project. Altho, that limits a number of frameworks that you can use. If you want to use more of them, you can add "Shared Project" to your project. That allows you to use more of platform specific code (for example Xamarin Components), but you will need to write code more consciously, because it may not work across all projects. You can read more about code sharing here
2) If you want to build app with different targets follow this instructions

EventHandlerList missing in PCL project but available on platform library projects (Android/iOS)

I have small problem - I'm trying to implement class that needs to contain a lot of events. Due to memory concerns I planned to implement EventHandlerList which is available for me in my Android Library project target also in my iOS Library project target but is not available for me inside PCL Project. Tried to change PCL Target project to most commonly used but none of them contained what i needed.
Type missing for my case :
System.ComponentModel.EventHandlerList
Is there any possibility to write such class once or I'm forced to write it two times because of missing PCL Target.
You will need Inversion Of Control (IoC) to use platform specific features or non-portable methods
Please take a look at this IoC example
Another option if you want to avoid IoC is to use Shared Project in Xamarin

Xamarin portable library - how to share classes

I'm building a project that includes an MVC Web Api hosted in Azure and an iOS app. I'm trying to use Xamarin to build the app. As I understand it, I should use a portable class library in my Xamarin project to allow me to share the code between my Web Api project and the Xamarin app, as well as any future apps on other platforms like android.
So right off the bat I would want to put my models in the portable library. The app and the web api will pass those models back and forth. But the portable library doesn't have the Azure Table Storage library. It doesn't even have some very basic stuff. My models need to reference the Azure Storage Library so I can save instances to storage.
What is the best way to make this code shareable? Obviously I need to duplicate my model classes so they can exist in each location. But should those in the PCL inherit from those in the Web Api project? Vice versa? Should there be an interface that both inherit from (actually the Azure Table Storage library requires the classes to inherit from ITableEntity already...). Just looking for the best way to share these classes between the Web Api project and the PCL used by the Xamarin project.
Using a PCL - Portable Class Library is a great way to get started! There are a few quirks that you may want to understand prior to sharing your code.
The PCL Profile is a limited set of APIs available. Meaning that certain classes/assemblies might not be included. You can typically look up the class/assembly via MSDN and see if it has a PCL icon next to the class name.
If the library you are trying to use has assemblies not inside the current PCL Profile but can be found on the native platforms, you will want to use the IoC/DI pattern.
Hopefully the library you're using supports PCL. Otherwise, you will need a library that does support the PCL Profile. (You can check this by downloading the .nupkg, extracting, and looking at the libs folder). Note: You may want to check the Prerelease NuGet channel for PCL support. Sometimes you can find an open source project and remove/replace certain assemblies/code to make it Portable.
General Guidelines:
Keep your POCO classes simple in the PCL. If you have platform specific quirks you need to add to the models, make a Model layer on that platform that inherits from your simple PCL models. EX: Your Web API has a specific [Attribute] tag or interface that you need to apply to your model. You might already have a Model such as Person which is a simple POCO class in your PCL, and then you can create a PersonApiEntity model which might inherit Person and any platform-specific APIs you need to apply to it.
It seems ITableEntity/TableEntity is not supported in the PCL Profile.
https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.itableentity.aspx
Seeing the source at a quick glance(https://github.com/Azure/azure-storage-net/blob/master/Lib/Common/Table/ITableEntity.cs)

Which one to choose Shared Project or PCL for sharing code?

Just wanted to understand there are couple of code sharing strategies exist to achieve code reusable capability in Xamarin.
Which one should i use ?
Shared Project way OR Portable Class Library way ?
if you can explain with scenarios , it would be very helpful for me.
Thanks much.
Here is the Xamarin explanation.
The question is possibly duplicated but you ask specifically for scenarios.
If you ever wrote c cross platform projects, shared projects resemble the old-school way allowing you to use #if __IOS__ statements to run device platform code in your shared/common code files. A separate assembly is created for each target (say iOS or Android). They give advantages and disadvantages of each.
PCL generates one single assembly for the common code. PCL has some limited number .net features as shown here in this table. However, most of the important .net goodies are there as you can see.
Xamarin says that shared code method is easier but PCL is easier to compile a module and share or sell that with others.
When I make projects, I check what external plugins/components/ etc I want to use and make a decision based from. For example, you may want to use sqlite and there are different options for using shared and PCL projects.

Resources