Hey I'm a Xamarin beginner who is a bit confused on how to structure my app to ensure cross-platform functionality while writing the smallest amount of code necessary. I have a library written in C and when using this library previously I would go the NDK route(for android), I now have to use the library for my Xamarin.Android and Xamarin.ios app. I want to know what the best way of achieving this goal is. Do I create a shared library or a PCL? Do I find a way to compile the library into a dll and just include in the Xamarin project?
Linking to C "native" libraries does not really change because you are using Xamarin, you still need to build all the various ARCH types, create universal/fat libs, etc...
On iOS you would be linking to a static lib or loading a .dylib:
iOS Linking/Interop Native Libraries
On Android you would be using your .so just like NDK (Xamarin.Android produces an NDK-based app):
Using Native Libraries on Android
*Note: If you are using Visual Studio, you also can debug your 'native' C/C++ along side your Xamarin.Android C# project.
In your C# code, in order to access methods defined in either one of those, you use Mono's P/Invoke functionality which is the same technology that you would use in .NET to access functions in C-based libraries.
Accessing C Methods from C#
Related
Hi when i am going to create new xamarin forms app I'm not able to see Protable Class Library in the Sharing Code Strategy options in VS 2017 .enter image description here
Please help me in this
That's the expected behavior, it's no longer possible to create a PCL library with Visual Studio 2017 as they've been replaced by .NET Standard libraries, see here for more details.
Also, the roadmap and the comparison with PCL profiles is available here, so you can determine the right .NET Standard version for you depending on your target framework and the APIs you need to place in the shared library.
.NET Standard libraries are the replacement for Portable Class Libraries. They allow you access to more of the framework than pcl did. You can still manually create a PCL project and reference it in a Xamarin app if you would like to use PCLs in new mobile apps with Xamarin.
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.
I'd like to build a PCL library, starting from an existing C++ code. This code has some dependencies (SDL, which is available as a NuGet package) and I'd like to know if it's feasible to integrate it in a Xamarin solution (which targets Xamarin.iOS, Xamarin.Android and Xamarin.Mac).
More precisely, the code is hosted here : https://github.com/chunying/gaminganywhere/tree/master/ga/client
If so, do I need to use C++/CX language ?
I have a project which greatly depends on ServiceStack, unfortunately the V4 releases, which includes PCL release for most platforms(especially after forking out for a Xamarin Licence), is just way out of my price range as an ISV. I have cloned the MvvmCross code but before I start messing around I would like to know what pitfalls I could run into when attempting to create a Non PCL based version compatible with the platforms I target (Currently Xamarin.Android, WPF and WinRT). The idea would be to able to reuse the code for the WPF project to build Android and WinRT clients.
Does anyone have any pointers?
I don't think there is any problem or pitfall with what you are describing - and I don't think you'll need to fork the MvvmCross source.
You should be able to build your apps as native code, and should then be able to reference the MvvmCross PCL libraries directly from the native libraries you are building - you don't need to rebuild MvvmCross to do this. Plenty of people already use MvvmCross using "file linking" rather than PCLs - it's not the default setup encouraged by the Nuget packages, but it still works fine.
I have a C# library that I want to use in my Xamarin project to deploy to an iPad.
Lets suppose the C# library is stored as MyUtilities.dll.
For a regular Windows app, I'd add MyUtilities.dll to my project Resources and then have a
using MyUtilities;
statement in the class where I wanted to use classes from that library.
How do I accomplish this same thing using Xamarin in an iPad app?
You will need to rebuild your library from the source using the Xamarin.iOS compiler. After that you would reference the same way you do in a Windows app - by either including a Reference to the dll in your project, or by including the Library project in your solution and referencing the project.