Implementing a dead reckoning application using Xamarin Forms - xamarin

I need to implement a dead reckoning application using xamarin. In order to do this, I need to access the pedometer/accelerometer from Android/ios and windows phone. Since they have their own APIs to be called, I believe the approach would be to create an interface in the PCL project and then have native implementations for each platform. These could then be made visible using assemblies. Apart from this, are there any approaches using which the entire code can be implemented in the PCL project itself? Without having native implementations for each platform?

The logic of your app can be done in the PCL but calling the pedometer or accelerometer must be done natively.
The basic approach is (just an example)
public interface IPedometer
{
int GetSteps();
}
and this interface is in your PCL.
On each native platform create a class (e.g. NativePedometer), implement this interface and natively code to get the result.
Use dependency injection on each native platform, e.g.
DependencyService.Register<NativePedometer>();
Then just use the below in your PCL.
DependencyService.Get<IPedometer>() and perform the business logic in your PCL. This allows you to reuse as much code as possible across platforms.
It is the best approach but you can't avoid writing native code and by native code I mean C# in the traditional/native Xamarin projects.

Related

Xamarin Cross Platform Development .net

We are developing a cross platform mobile application using Xamarin. I have noticed there are different ways of structuring your code using Shared code or PCL/.net standard.
I attempted to use .net standard and what i have found is that it doesn't include all the libraries such as using ado stuff , datatable/datasets using system.data. Or using system.net for example. Other functions have less overloads and seems that its a stripped version of .net to work across platforms. Is there any way to add this functionality or use a broader range of .net?
You are correct - .Net Standard and .Net Core are stripped down versions of the full .Net framework. They contain only features that can be used on all platforms (Mac, Android, iOS, Linux, Windows, etc.)
The full .Net framework only needs to work on Windows, so there are additional things that you can do based on the fact that there is less complexity in implementing and that the code base is more mature (cross platform is still pretty new by C# standards). To the best of my memory, System.Net should work if you have your references correct, but System.Data will not (or at least direct database access is not possible)
You can regain some functionality through NuGet packages, but it won't be the same as using WinForms or something like that...
My best advice would be to program against an API. You can use a web server that runs on the normal (full) .Net framework, and does all the heavy lifting using all the .Net features that you are accustomed to. Your Xamarin app would send and receive data from the API and basically provide a mobile front end for whatever you are doing. This approach makes sense for most apps anyway, and is generally what you would be doing anyway if you used Swift and Java to create separate iOS and Android apps.

What's the difference of Class library and windows runtime component for UWP

I'd like to create a custom charting control for my data, it will be used by uwp store apps, in Visual studio, there is class library project and there is also windows runtime component project, what's the different? which should I use?
Also, user control or template control, which one should I use?
It all depends on what programming language you plan to use, how complex your control will be, if it will be only you using it on a single screen in a single project or other people might use your library and try to restyle the control.
In most of the cases you'll be picking a class library (if you're a C#/VB developer) and go for the templated control for maximum flexibility in styling (and better performance than a user control). This answer by Filip Skakun gives an excellent comparison between both types of controls.
If your custom charting control has to be used in UWP apps written in C++ or JavaScript (WinJS) or is written in C++, you'll have to use a Windows Runtime Component. A class library can only be used in .NET UWP apps. More info on Windows Runtime Component and how to create them is on MSDN.

Porting android app to xamarin or windows native questions

So, I have been developing an android app for some time, and I was requested to try porting this app to windows phone too. In order to reduce the hassle in trying to maintain two separate versions of the same app, I decided to try to port this app into xamarin, because I have heard that its performance is better than hybrid apps. These are my questions with regard to xamarin:-
I want to maintain the same look and feel which I had in my original app into my cross platform app, and at the same time, make it distinct in the windows version. What are the things I can do to achieve this effect?
Is it rather better for me to port this app to windows phone native, because of added problems in trying to create a common app?
Are there any restrictions with regard to Windows store when publishing xamarin apps?
Xamarin doesn't really cover Windows UWP apps, only in the notion that they have the UI Framework Xamarin.Forms running on that platform. Meaning, that what you are making is a native Windows UWP app.
What Xamarin is great at is when you start targeting more than one platform and you have structured your code in a way that it can be reused on the supported platforms. This could for instance be done by putting most of your logic and behavior into a Portable Class Library/NETStandard library and consume it in your apps.
A Typical pattern for making platform agnostic logic and behavior for your apps, is the pattern known as Model-View-ViewModel, where the View is platform specific, while the Model and ViewModel usually are platform agnostic. The ViewModel is where the behavior resides and it is what ties the Model together with the View.
Usually the ViewModel wouldn't directly know the View, but there would be a layer in between (glue), such as XAML or a binding engine from MvvmCross, MvvmLight or ReactiveUI to name some MVVM libraries.
What Xamarin provides is the ability to write C# code for Android and iOS, which greatly enables you to share code between those two platforms, but also all the Windows platforms. Hence, UI, is very much dependent on each of the platforms on their own.
You can, of course, use Xamarin.Forms as a UI abstraction layer, which produces a native UI using the native UI controls to get a similar app on all the targeted platforms.
First of all you need to know there are different styles of Xamarin development, who will share more or less content.
If you use Xamarin Forms you have a Main project non-related to any platform (where you create the views and clases), and specific platform projects who adapt the controls to each native style.
If you develop using Xamarin Classic, you have a Shared project where you only develop data-related classes, and specific platform project with their own views and classes with native-friendly controls and native similar functions, but I think, there is no direct Xamarin Clasic Windows Project.
So if you only want to have two apps who look native both, but with same structure and functionalities Xamarin Forms will be the best option for you, cause you only develop "one single app" who becomes native-style like this:
If what you want is to have different apps, with different functionalities and diferent content, then you need to go for Xamarin Classic. What I recomend you to do then is develop the windows phone in native, but put all of the code you can in a shared library. Then you can create a Xamarin classic Android app and use the shared library. You will still need to mantain two different apps, but you will only need to change the "core" code only one time.
If you use Xamarin Forms to do a UWP windows app I don't think you have any problem to publish it, think Xamarin is from Microsoft.

How to create cross platform xamarin library which uses phone's GPS module?

I am a beginning JS programmer developing in Angular but now I got assigned this task: create a library that accesses phone's GPS module, gyroscope and accelerometer (and possibly a compass) to gather acceleration, position and some other data. It's not a whole lot of code really.Now, creating the library itself in C# is already done by a colleague but I need to know if there' s a way to include objective-C and java code as well into the library which would be the platform specific code for ios/droid respectively.
The result literally has to be a single dll file and it must be useable in xamarin. The requirement even was for the library to work with angular which I have no idea if is even possible. It would be a bonus if it could.
What options do we have in accomplishing this?
You need to create a portable class library (PCL) that targets all platforms you want. In this PCL you'll have an interface that needs to be implemented in every platform, using platform specific code. Say:
public interface IGps
{
int SomeFunc();
}
Every code that manipulates the GPS info will do so in the PCL using this interface.
In each platform you want to target, you'll need a class that implements this interface and injects it into the PCL using a Dependency Injection framework. This way you'll be using platform specific from the PCL without it knowing the implementation details for each platform.
public class GPSWindowsPhone : IGPS
{
public int SomeFunc()
{
return 1;
}
}
As for using each platform's native code, it can be done using bindings for Obj-C and Java code and then using the class that implements the interface as a simple wrapper for the code written in the native language. However, if you still haven't done any platform specific coding I'd recommend writing them in C# already.
Regarding Angular, you can look at some [language] to JS compilers, but I'd just rewrite it :P
There are already a number of Cross Platform Libraries in the Component store.
Xlabs has one
https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator
Gives you Lat,Long and Heading in an event driven model you can then use to get speed direction acceleration etc. Works in Xamarin on IOS Android and WP.

Xamarin.Mobile crossplatform implementation

I learnt that Xamarin.mobile provides single set of APIs across platforms. But implementing in a cross platform app, how does it look like?
A cross platform architecture consists one project containing shared codes (core project) and other project consisting the UI codes per platform. So, as Xamarin.mobile provides a single set of API, how can we utilise it to write once and use everywhere? Eg: implement mediapicker.TakePhotoAsync in one class and use it in multiple platforms without writing the same code in other platform. Because, the sample app i downloaded from Xamarin.mobile consists of apps developed for different target individually (not cross), though i see they use the same media picker, geolocation and contacts namespace across different platforms.
Thank You!
Xamarin's 'traditional' approach to cross-platform apps has been to use file-linking.
I believe this is currently changing to a dual approach of recommending either:
file-linking
or
Portable Class Libraries (PCL)
The first version of Xamarin PCL support is due very soon (any day now).
When that becomes available and stable, then I expect you may see new versions of xamarin.mobile which are available for PCL. Also, they have indicated that they may open source xamarin.mobile. As it stands today, I don't think xamarin.mobile is a very active development - I don't believe it's changed/grown much in the last year.
Until these changes happen, then if you want to use xamarin.mobile from portable code, then your best bet may be to either follow the file-linking route, or to use PCL libraries but use some form of facade to wrap the native xamarin.mobile libraries inside an injectable interface

Resources