I ran into an issue where my Xamarin.Forms UWP app, which uses MvvmCross and SyncFusion SfCharts stopped showing charts once I created the app package for the Windows Store (i.e. created *.appxupload package with .NET native tool chain).
I went hunting for a fix and rather quickly found a post on the SyncFusion official support forum about that problem. I also found that there’s a official Knowledge Base article:
Both saying the same thing, that one should go to their App.xaml.cs file and add an extra assembly (i.e. typeof(Syncfusion.SfChart.XForms.UWP.SfChartRenderer).GetTypeInfo().Assembly) to their Xamarin.Forms.Init() parameter in the OnLaunched() method. Basically add this code:
Unfortunately, this doesn’t work with MvvmCross 6.1.2 (probably doesn’t work with 6.2.0 as well – haven’t checked though). The charts are still not showing, even though the SfCharts assembly got included.
I can’t be 100% sure of the reason behind this problem but I suspect it’s because of the fact, that MvvmCross framework is now handling the Xamarin.Forms initialization for you. Because of that the Xamarin.Forms.Init() method is called multiple times – once in the App.xaml.cs and once inside of the framework. Another assumption, based on the results we’re seeing, is that Xamarin.Forms.Init() works in a way that the last in is the one that gets served – meaning that if the App.xaml.cs call happens before the MvvmCross one, then the SfChart assembly is not going to be included and Native tool chain compilation will strip it out.
A bit more explanation on this: Mvx framework calls the Xamarin.Forms.Init() method inside of the base Setup file – MvxFormsWindowsSetup calling Xamarin.Forms.Init(). The remedy is simple, move the code from App.xaml.cs to the GetViewAssemblies() override in your Setup.cs file. The GetViewAssemblies() method is being called at the launch of the app by MvvmCross and it is designed to retrieve additional assemblies to pass as a parameter into the Xamarin.Forms.Init() method – which is exactly what we want.
I’ve stumbled upon this issue straight after I released my app into the store…
I did a brief search on the Internet what could be causing such behaviour and majority of posts were pointing at some issues with cached data by Google Play Store and/or Google Play Services somehow breaking installation process. They were suggesting cleaning cache of these to solve the problem. Combination of clearing cached data, uninstalling the app, restarting the device and even ejecting the SD card did not help
I didn’t give up though. I was fortunate enough to have a device at home, that I was able to reproduce this with. At least it showing some errors when trying to deploy and debug the app using Visual Studio – which was a good start. Each and every time I tried to deploy the app onto the device I was getting the error
ADB0010: Unexpected install output: Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED: Failed parse during installPackageLI: /data/app/vmdl1021324914.tmp/base.apk (at Binary XML file line #28): <service> does not have valid android:name]
It didn’t tell me much, so I started googling it. Some of the StackOverflow posts (#1, #2) that I found were pointing me at the problem with package name capital letters. Yep, I did put some capital letters in my app name (i.e. com.progrunning.AirQuality, which ultimately became com.progrunning.airquality). I changed the name of the app in the AndroidManifest.xml but I was still getting that error…I had no choice but to check what’s actually being generated/added by Xamarin.Forms into the AndroidManifest.xml when packaging the app. I went to my bin\Debug folder, to try to unzip the generated *.apk file – *.apk is just fancy *.zip file. Unfortunately, at this stage the AndroidManifest.xml file in the *.apk is nothing but binary data. Which looks something like this when opened with Visual Studio Code:
Fortunately, thanks to this StackOverflow answer, there’s a way of reading what’s in the *.apk package. I located the aapt.exe file, where my android sdk got installed (in my case it was C:\Programming\SDK\Android\android-sdk\build-tools\27.0.3) and ran the command
aapt.exe l -a “C:\Programming\Projects\Own\AirQuality\AirQuality\AirQuality.Android\bin\Debug\com.progrunning.airquality.apk”
The above prints out quite a bit of stuff (you can probably narrow it with some pipes and grep commands – I’m no expert on these though), but we’re interested only in the AndroidManifest.xml file. From the error that I got when trying to deploy and debug the app I knew that something that prevents this from happening is located at the line 28. I finally got my answer:
I used capital letters for my background service (biggggggggggggg mistake!):
Changing it to lowercase fixed the issue
EDIT: The problem didn’t seem to be happening for the Android versions 8.0+. It was only affecting 6.0 and 7.0 (maybe more but these two versions are definitely confirmed)
EDIT2: I ended up with my app name having capital letters in it – that was not the problem
This topic is going to be sort of continuation of my previous post about SQLiteWrapperUWP-PCL – an SQLite Wrapper for Windows 10 UWP applications. I’m writing “sort of” because it’s actually an update to that post. The project part – setting up the solution – hasn’t changed but the code part has. Please understand that the SQLiteWrapperUWP-PCL library is being constantly developed and improved, hence I need to explain how things has changed and how one should implement their database logic with latest release of SQLiteWrapperUWP-PCL NuGet packageBuildIt SQLite.
For those who are not very familiar with Windows 10 and UWP application development please, before you start going through what’s written in here, refer to my previous post about SQLite Wrapper for Windows 10 UWP applications, where you can find guidelines how to set up your solution / project.
EDIT: Apologies to all that wanted to try out the SQLiteWrapperUWP-PCL and some of the links weren’t working, but SQLiteWrapperUWP-PCL evolved from its own existence, to become a part of a bigger set of libraries, called BuildIt. Those libraries are a foundation that we rely on, in almost every project that we create @BuiltToRoam, so besides working with SQLite check out others, I can bet you will like it and use it.
Let’s start from the Core project and setting up the DatabaseService.
The base class, that DatabaseService inherits from, is an abstract class BasicDatabaseService which is a exemplary implementation of BaseDatabaseService. Both of which sit in the SQLiteWrapperUWP-PCL library and expose the IBasicDatabaseService and IBaseDatabaseService interfaces. In this case, deriving from Basic service, one have to provide only the implementation of CreateDatabaseTables method, which is responsible for creating database tables, and provide some constructor parameters, about which in a second.
I think it’s worth noting that one is left with an option to derive directly from BaseDatabaseService and in that case it opens a path where one could provide their own SQLite database connection creation (implement CreateSQLiteConnection method) handling.
Let’s explain a bit about DatabaseService constructor parameters and its purpose.
ISqlitePlatformProvider consists of one child, and its purpose is to provide core SQLite platform functionality, basically it’s the “heart” or “spine” to the SQLite database.
IDatabaseNameProvider is nothing more but a “fancy” way of saying “if you want a database you need to give it a name it”. With this interface implementation you need to provide a database name, and that’s it – one string
Last but not least is the ILocalFileService which is necessary to provide a way of getting physical path – RetrieveNativePath method – where the database file will be saved (on a hard drive)
NOTE: In almost all of my projects I use mvvm cross-platform framework MvvmCross, which I highly recommend, and with it, out of the box, comes Dependency Injection (IoC) mechanisms. Those mechanism under the hood, create and inject automatically singletons for mentioned above interfaces (constructor parameters). You can find out more about it in the N+1 days of MvvmCross blog posts and videos. For those who are familiar with it and to spare you some time looking for how it could be initialized, here’s a sample code from Setup.cs file
Important thing, that has been changed in the latest release, is BaseEntity class. It was redesign to consists the update of representation of the entity logic (update of the database record). For example:
There’re two things to note in the above code. First is to do with self-reference generic class in the class declaration. It was purely designed this way so we could handle the update logic inside of the Entity class, which is called from the BaseRepository every time the Update logic on the entity is executed. UpdateFromEntity method – second thing that one should be aware of – should be a place where entity update is being handled.
Assuming you have an instance of DatabaseService handy (e.g. created with MvvmCross DI mechanisms) and with all this setup / knowledge we can now start working with our database, and it couldn’t be any easier. To create new database record (entity) one could write something as follows:
Those are Base CRUD methods, but if you’d like to write more sophisticated queries you should look into BaseRepository and Table property. It will allow you to perform some LINQ queries on it. For example:
NOTE: As it’s not perfectly obvious, the CreateSQLiteConnection method won’t create a new one every time it’s being called, but it will take the cached one.
NOTE: Another thing is that not all LINQ queries will be valid in terms of translating them to what database understands, hence sometimes it’s better to grab all table records (entities) and perform some queries on the values stored in memory.
I would highly recommend checking out the GitHub repository and the sample that is in it, as it consists all of what I’ve just wrote about, and you can easily compile and debug that code.
NOTE: For inspecting SQLite database file(s) I recommend using SQLiteBrowser
The idea for SQLite Wrapper was purely dictated by the demand. We, at BuiltToRoam – company that I work at – are dealing with all types of universal application projects (Universal Apps for Windows 8.0-8.1 and Windows Phone 8.0-8.1 or UWP for Windows 10) that are required to use database(s). With every new application, we were basically re-using same code that was written for one of the previous apps. It was done in a really “savage” way, by copying and pasting core functionality and adjusting it accordingly to the needs of the app. Not good..something had to be done. The decision was made. We created a PCL (Portable Class Library) wrapper over SQLite.Net-PCL library that implements some basic functionality for Creating, Reading, Updating and Deleting (CRUD) operations on the database tables.
SQLiteWrapper came to life on a GitHub repository. From the beginning of it’s existence in the Open Source space I was tempted to create a NuGet package out of it. With NuGet package it would be much easier to distribute it and share it. On top of that, nothing would change in terms of GitHub repository, it would still be there so anyone could grab sources. Besides..I always wanted to have mine NuGet package, even though this one wouldn’t be strictly speaking mine. Even though it is a child that whole BuiltToRaom team gave birth to, I feel like at the moment I’m changing its dippers ;]. What’s more, creating this package could finally place myself on the map of .NET world!
I ended up reading some articles how to create a package, what should it consist in terms of information and description and a bit later..I present you SQLiteWrapperUWP-PCL! There’s still no information whatsoever about how to deal with it or how to use what’s inside of if. I will try to fill this gap by guiding you, reader, through the process of creating a really basic example of SQLiteWrapperUWP-PCL usage.
The SQLiteWrapperUWP-PCL example
Let’s start with creating an empty solution in Visual Studio 2015 – I’m using Community edition.
Next let’s add UWP project
Next add a PCL (Portable Class Library)
As you can see on the support selection screen (it appears after clicking OK on the dialog with Add New Project and choosing PCL), I’m selecting .NET Framework 4.6 and Windows Universal 10.0 (UWP).
After those steps you should end up, in your Solution Explorer, with something similar to this
Having that done, we can grab NuGet packages and add it to the project. As SQLiteWrapperUWP-PCL depends on the SQLite.Net-PCL library we it will be installed along side SQLiteWrapperUWP-PCL
New references should appear in both Core and UWP projects and in the NuGet Package Manger window green tick should appear next to libraries that were installed. Even after successful installation you still don’t seem to have SQLite.NET-PCL library referenced (in one or both of your projects) you should try installing it explicitly (by finding it in the NuGet Package Manager and installing it from there)
You’re probably aware that to work with SQLite database one need an SQLite extension. In our case it’s going to be UWP extension. You can download one from Visual Studio, by going Tools –> Extensions and Updates and then selecting on the left side Online –> Visual Studio Gallery and search for sqlite for universal
After downloading SQLite for Universal App Platform you will be prompted with installer confirmation dialog
Remember to restart Visual Studio after installation has finished. Now you will be able to Add reference of this extension to your UWP project.
Then navigate – left side panel – to Universal Windows. SQLite for Universal App Platform should be listed in Extensions
Click OK. Afterwards you should have your SQL extension visible under References in your UWP project
At some point you should add a reference to the Core (PCL) library in your UWP project. You can see that I already did that, look on the above screenshot, it sits just below the SQLite for Universal App Platform reference.
Last thing, that is not necessary but recommended is to install MvvmCross libraries. It can be done in the same way as we did with SQLiteWrapperUWP-PCL, which means that in the NuGet Package Manager find MvvmCross and install it in both projects. On the below screenshot you can see MvvmCross already installed
Installation of MvvmCross framework will most likely create some folders and files in your project. To get familiar with MvvmCross and it’s structures, concepts and files I would recommend reading and/or watching some videos from the N+1 days of MvvmCross blog posts.
In terms of preparing your project for this example that should be it, let’s proceed to the code part
The description, below, was for version of SQLiteWrapperUWP-PCL up to 184.108.40.206. For the new version you can find some information in my latest post on this subject
Let’s start from the Core project and setting up the DatabaseService.
The base class – BaseDatabaseService – is the one that sits in our wrapper and is responsible for database management (e.g. creation, opening or closing). To create either BaseDatabaseService or in this case DatabaseService one will have to specify a constructor parameter of ISqlitePlatformProvider, I will explain that one in a moment. In the above code besides the constructor you can see an override of the CreateAndOpenDb method. It allows as to “plug in” between the creation of the database and returning connection to it. It’s done this way to make sure that before someone will start operating on the database (SQLiteConnection) they will have the proper entities – database tables – defined in it. In this case PersonEntity table is being created and afterwards dbConnection returned. Invoking dbConnection.CreateTable<PersonEntity>(); will map the model of our class to the database table. For more information about creating tables and mapping in general I’ll refer you to the SQLite.NET-PCL git project site, where you can find some examples.
PersonEntity is just an example of how you should structure your database entities. They should inherit from BaseEntity class so that you could use BaseRepository implementation – both of those classes are in the wrapper and about the second one I will talk in a moment. You should know that BaseEntity has a Id property which is decorated with [PrimaryKey] attribute and as you may suspect it will be mapped as a database table PrimaryKey.
Before explaining ISqlitePlatformProvider and BaseRepository concept I’ll quickly show you how I dealt with PersonService and Inserting and Retrieving data from database. Let’s start with very simple IPersonService interface that our service will implement
As simple as two methods, Insert and RetrieveAll. Implementation will sit in the PersonService.
Constructor takes SQLiteConnection which is the product of database service CreateAndOpen() method call. There’s also Insert and RetrieveAll implementation. Both use same pattern, which is BaseRepository<TEntity>. Repository part of the wrapper is the “thing” that you will deal with the most of the time. In above snippet of code I’m simply creating, for both methods, new BaseRepository<PersonEntity>, within using statement – because BaseRepository implements IDisplosable interface –which allows me to operate on my table (e.g. personRepo.Insert(person) or personRepo.Table.ToList()). Explore BaseRepository more to get familiar with functions that we exposed (e.g. Get or Delete).
That’s about it when it goes for Core, let’s move to the UWP project. Starting from mentioned earlier ISqlitePlatformProvider
That one is pretty easy. It’s purely about platform specific implementation of SQLite.Net-PLC library and how it will handle all the “low level” SQL database stuff. In this case our platform will be SQLitePlatformWinRT.
Last but not least is to put all this together and insert and retrieve some actual database entries.
I hope the comments and my walk through will let you understand what’s happening in this snipped of code. If you still have doubts you should definitely try it yourself! Start from checking out this SQLiteWrapperUWP-PCL example project that you just read about.
NOTE: For inspecting SQLite database file(s) I recommend using SQLiteBrowser
This issue stroke me suddenly and without any reason (at least I couldn’t find one). One day code that rotates picture, which was captured from camera, started to throw exceptions. I was really surprised, because I didn’t change much in the project since the time it was working just fine..Till this day, I have no idea what was the cause of it
What I figured out after some research on the Internet and looking in the generated *.appx package files (appx ~ zip – by changing extension of your *.appx to *.zip you can check what’s in it) I discovered that for some reason the AppxManifest.xml was lacking entry with the title library mentioned in the title of this blog post (highlighted entry is the one that was missing and had to be added manually)
<ActivatableClass ActivatableClassId="Lumia.Imaging.StorageFileImageSource"ThreadingModel="both"/>// This was missing
I couldn’t work it out how I can force mechanism standing behind creating packages (*.appx) to include this in the AppxManifest.xml, so the only solution was to insert it after package was made. To automate this process, of unpacking files from *.appx package, making amendments to the AppxManifest.xml and then repacking again all the files into *.appx pckage, I wrote a pretty ‘nasty’ PowerShell script which uses makeappx.exe (to unpack and repack) and some other PowerShell ‘hacks’ ;]
# - Extracts createded package (*.appx) with makeappx.exe
# NOTE: Takes first *.appx file that it finds in the folder that script is located in
# - Opens and adds, by replacement of known entry, missing entry in the AppxManifest.xml file