Adding widgets in Android project can show widgets in IOS using XAMRIN - xamarin

On making a new project solution in XAMRIN, there are 3 projects were created
1. ProjectName.Portable
2. ProjectName.Android
3. ProjectName.IOS
Can I add widgets in the Android project and on running the app, will that added widgets added in Android be showing in the IOS and WINDOWS apps? The purpose is only that I am familiar with Android development but in Portable project, the development is a bit different than Android.

Short answer, no.
Longer answer:
Depending on what project type you choose you define your views differently.
If you choose Xamarin.Forms, typically you would define all your views in the Portable project and changes will be reflected in both your Android and iOS project.
If you choose a classic Xamarin app, you need to define views per platform. So you would define Views, Activities and Fragments on Android.
On iOS you would similarly define Views and ViewControllers.
In short terms, Xamarin apps are native apps, written in C#. Hence, unless you add abstraction, you won't automatically get write once run anywhere functionality.

You should not reference code from different platforms. Instead, reference portable project from every platform. In portable project place some cross-platform code that is independent from where it runs. And place platform-dependent code in platform projects.
Use Xamarin.Forms in your portable project to build cross-platform UI. If you need to, use Xamarin.Android to operate Android-specific UI elements with Renderers. Do last step for every platform you need.

Related

How to find code targeting an SDK version of Xamarin iOS at compile time

My app is built against iOS 11 SDK but the deployment target is set to 9.3. So, the app builds but if I don't see that there's a code that use a method only available on iOS 11 (or a version above 9) and I run it on an iOS version 9 the app crashes. So, is there a way to get all the code incompatibility with a certain version of iOS?
** Edit: New answer after reading title more carefully.
Xamarin iOS does not appear to define anything automatically for the deployment target.
If you must do it at compile time, you can define your own symbols in the compiler settings. For example you can define IS_IOS_10_OR_GREATER, then just change that for different projects.
The official advice from Xamarin is to make the determination at run time since the version of iOS you're running on is what determines capabilities. You can use conditions around code using:
UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
UIDevice.CurrentDevice.SystemVersion
Another option is to create an interface and have different implementations depending on the above method calls, then select which implementation accordingly. Ioc would be very useful for this.

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

Extracting common code from Xamarin Android project

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.

How to reference System.Net.Sockets.Socket in PCL

I have been developing an app for Android, Ios and UWP.
Since I need a socket connection, I used DependenyService to access the "platform specific" code and started to implement my interface in all 3 projects.
The problem is that the implementation is exact the same in all 3 projects, because they all use System.Net.Sockets.Socket.
However I can't simply put the implementation code in my PCL and use it for all 3, because I can't reference System.Net.Sockets.Socket in my PCL. (doesn't exit there)
This picture shows the targeting section of my PCL
I think the problem is that my PCL targets ASP.NET Core 1.0, which doesn't contain an API for System.Net.Sockets.Socket.
However, I can't tell my PCL to stop target ASP.NET Core 1.0, because it gets targeted automatically.
So has anyone an idea how I can share code only between Android, Ios and UWP, or make my PCL only target them?
If the app should only support iOS, Android and UWP, you can switch to the PCL of the year 2016 and the future called .NET standard (>= 1.3).
In the properties of your PCL click Target .NET Platform Standard and select 1.3 or higher. If you don't have this option, you have to install/update some stuff. Requirements are listed here: https://learn.microsoft.com/en-us/dotnet/articles/core/tutorials/libraries
After you have done that, you are ready to use Sockets without using patterns like factory or dependency injection (which are the alternatives).
Or you use the Sockets plugin: https://www.nuget.org/packages/rda.SocketsForPCL

Recommended Cross-Platform (Windows and Android) Project Set Up using OpenTK

I am starting to develop a scientific software that I hope I will be able to run on multiple platforms. My plan is to use OpenTK for the rendering of the scientific models and plots. As of the moment I have a prototype that runs on Windows using OpenTK 1.1 libraries from http://www.opentk.com/ (a simpler version just with OpenTK and a more complicated one with OpenTK + WindwosForms). I am trying to port that prototype to Android.
It seems that the syntax using by the Xamarin.Android OpenTK library is nearly identical to the one that I am currently using for Windows (with the only difference that OpenGL -> OpenGL ES and GameWindow -> AndroidGameView) so the porting shouldn't be an issue. However, I was hoping that I could avoid a copy-paste method and get a more permanent solution having a shared OpenTK code between the Windows and the Android version.
I have read trough the Xamarin documentation about the shared vs PCL methods for cross-platform development. However, I still struggle to figure out how to set-up a Visual Studio solution with an Android and Windows project and a shared code that will include OpenTK. Is that even possible and can someone give me an example of how to do it? I did explore an example I found for rendering a rotating cube using OpenTK for a shared Android/iOS project (http://developer.xamarin.com/content/TexturedCubeES30/) but in my case I need to use a different OpenTK library for the Windows and for the Android project.
I also found this Do the Android and iOS versions of OpenTK have the same API? discussion. It is very similar to what I would like to do but in my case I am trying to setup a project for Windows and Android (for now).
Can I use only one OpenTK library (which one?) that is being called from both the Android and the Windows project and what will be the right way to set-up both projects so they share the same OpenTK code. This is the first time I am dealing with writing a cross-platform code so I am a bit lost.
Edit: I was able to get a prototype running using Shared Xamarin project and compiler flags as proposed below. Code was indeed not very pretty at places but I got over 70% code re-usability between the two platforms so it was worth the effort. This is how I used the compiler flags in case someone is looking for the same thing (credit to SKall from the Xamarin forums):
#if __ANDROID__
using OpenTK.Graphics.ES11;
#else
using OpenTK.Graphics.OpenGL;
#endif
I used the #if syntax similarly where there were small differences between the syntax of the routines.
It does not seem like OpenTK has its logic inside of a PCL in the first place, so your plans on putting it there are going to get hard to achieve.
However, if you split out your code, such that most of it is contained in classes, which are not highly dependent on the underlying platform, you will be able to create a Class Library Project for each platform and link your files between the platform specific projects. Inside of the classes it contains you will use #if definitions to choose whether to use AndroidGameView or GameWindow and the same goes for other platform specific types. It will make the code ugly, but this is the alternative to PCL.
You could try to see how much of the OpenTK code compiles inside of a PCL and inject the platform specific stuff at runtime, but it will require considerably more work from you. However, it will make the code a lot more cleaner to look at.
To ease the file linking, you could make one of those Shared Projects and chuck in all of the logic in there.
Some more info about code sharing here: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/
Dependency injection: https://en.wikipedia.org/wiki/Dependency_injection

Resources