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
Related
How do your write Xamarin platform specific code in a .net standard library?
I want to use namespaces like Xamarin.Forms.Platform.iOS in a .Net Standard Library..
Use case: I want to develop a .net library for my apps which includes a video player for the various platforms. This video player also has to interact with other code in the .net library.
Or is the answer I need to use a shared project or portable library?
You should not include platform specific code or use namespaces like the one you mentioned in your .NET Standard Library, the reason is that .NET is just a runtime environment.
It’s not the the main runtime environment that you would use on iOS or Android. These platforms use Mono - not .NET. Check this or this to see more details for how its structured.
If you need to execute something from your .NET Standard project which is related to platform-specific behavior, use Dependency Injection or Custom Renderers.
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.
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.
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)
I've been playing about with the monodroid (preview 8980) and I'm trying to create an application that will run on Android, WP7 and silverlight.
My plan is to create a single core class library and then a seperate project for each of the platforms that will contain the UI stuff - so one monodroid project, one for WP7 and a SL one, all of which will reference the core class library.
The main problem with this is that it will be possible to implement some functionality in the core library that will work fine on silverlight, but not on WP7 for instance. I believe the best way to make sure this doesn't happen is by making the core library a silverlight 3 project, as this will be the lowest common denominator.
The problem I am facing now is that I can't reference a SL3 library from the monodroid project. I get this warning - 'Warning 2 The project 'TMCore' cannot be referenced. The referenced project is targeted to a different framework family (Silverlight)'
Any ideas?
Bah I seam to always end up answering my own questions - http://www.gregshackles.com/2010/12/shared-libraries-for-windows-phone-7-monodroid-and-beyond/
according to
http://monodroid.net/Documentation/Assemblies
you must compile your core-stuffe into a seperate assembly you cannot share a core-dll.
> Note: MonoDroid is not ABI compatible with existing assemblies compiled for
> a different profile. You must recompile your source code to generate
> assemblies targeting the MonoDroid profile (just as you need to recompile
> source code to target Silverlight and .NET 3.5 separately).