Xamarin Phone Application w/SQLite
Download this Project @ GitHub
Posted on September 12, 2020
Xamarin
Xamarin is a free open source cross-platform for building Android and iOS phone applications with .NET and C#. This allows a number of useful .NET tools such as Lambda expressions, Language Integrated Query (LINQ), and Asynchronous programming ( async/awit ) to be incorporated. Xamarin uses C# and native libraries wrapped in the .NET layer for cross-platform app development. C# is a mature language with strong safety-typing that prevents code from unexpected behavior. Xamarin can leverage all native and even the latest APIs to utilize underlying platform capabilities in Xamarin apps such as ARKit on iOS or Android Multi-Window.
The runtime behind Mono and .NET for Windows is based on a Just In Time (JIT) compiler [fun fact: Xamarin is the company that develops Mono]. C# and other .NET languages are compiled into Microsoft Intermediate Language (MSIL). MSIL is compiled into a native code to run on the type of architecture that is running the application at runtime. Xamarin.Android is able to follow this exact pattern, but due to Apple's restrictions for dynamically generated code, a JIT compiler is prohibited on iOS. Xamarin has developed a new option called Ahead Of Time (AOT) compilation to work around this restriction. AOT has made .NET possible on iOS and brings other benefits such as a shorter startup time and potentially better performance.
The code related to business logic, database access, and network communication can be shared across all platforms, but Xamarin allows there to be a UI code layer per specific platform. Xamarin cross-platform apps look 100% native on any device. Xamarin.iOS applications can be published to the App Store for download or purchase. An Apple Developer License is required to publish to the store; the cost is $99 for a single hobby developer.
Zxing.Net.Mobile.Forms
ZXing.Net.Mobile is a C#/.NET library that makes scanning barcodes effortless and painless for application development. Add the nuget package to the shared code project. Add this line to the OnCreate method in the MainActivity.cs located in the Android project.
ZXing.Net.Mobile.Forms.Android.Platform.Init();
Add this method to the MainActivity.cs located in the Android project.
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
Add the camera and flashlight permission to the AndroidManifest.xml file in the Properties folder in the Android project.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
Add this line to the FinishedLaunching method in the AppDelegate.cs located in the iOS project.
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
Add the camera permission to the info.plist by adding the NSCameraUsageDescription key in the iOS project.
<key>NSCameraUsageDescription</key>
<string>Please allow access to the camera to scan barcodes</string>
Create the view that will allow the end user to scan any Barcode or Quick Read (QR) code.
scanView_OnScanResult method handles what the application does with the result of the scan. This would be where it could be tied to an API or database. Xamarin.Essentials can detect if the device is connected to the internet. If so, then a user could 'synchronize' their data with the Production server through a series of POST calls. This application stores the data locally with SQLite.
SQLite
SQLite is a Relational Database Management System (RDBMS) contained in a C library that implements a small, fast, self-contained, highly-reliable, full-featured SQL database engine. SQLite is not a client–server database engine; it is built into mobile phones and computers. The source code is in the public-domain and is free for everyone to use for any purpose. Go to the Browse tab in the NuGet Package Manager to search for the sqlite-net-pcl NuGet package and add it to the unified project. Then create the class for the objects that will represent a row of a datatable.
Next create a Database class for the project. This is the CodeItemDatabase.cs file in the Data folder. Double-click the App.xaml.cs file to open it from the unified project to add the Database. This implements the Database and can now be used through LINQ or SQL statements to update the data.
Finished Product
The resulting project allows the end user to scan any code and store it to the phone. The user can choose to create, delete, or modify any record. A quick Preview option was created for each record. This project can be a starting off point to be used for an Asset Tracking Management System, Logistical Shipping, Warehouse Receiving, etc. The application could be modified to hit an API to reach a Production server and flow through the Company.