How to use SQLiteAsyncConnection from the async PCL version of SQLite? - c#

I'm using a PCL version of Sqlite.net from https://github.com/oysteinkrog/SQLite.Net-PCL
However, I'm unable to get the DB connection setup. The SQliteAsyncConnection does unlike the original version not take a string (path to the DB), but a [Func< SQLiteConnectionWithLock>.]2
How can this be used?
In general: how to use this library? I have a core PCL lib which does all the business logic from my iOS, Android and WP8 projects. My understanding was that I can drop the the Sqlite-Net Async PCL into my PCL lib. But it seems like I have to provide some platform specific stuff to get it to work.

You just need to create a function that returns a SQLiteConnectionWithLock and pass that to the SQLiteAsyncConnection constructor.
string databasePath = "path";
var connectionFactory = new Func<SQLiteConnectionWithLock>(()=>new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString(databasePath, storeDateTimeAsTicks: false)));
var asyncConnection = new SQLiteAsyncConnection(connectionFactory);

I've had problems with this too. To meet Johnbot's answer which is correct I am adding how I have used this library and got it to work. I did not really understand the NuGet packages, so I did it manually.
Download the project
https://github.com/oysteinkrog/SQLite.Net-PCL
Load the solution in VS2015, set to release mode and create the dlls for:
a) SQLite.Net
b) SQLite.Net.Async
c) SQLite.Net.Platform.WinRT which is for Windows 10 universal apps, or the platform you need.
Ensure you have these references in your project.
SQLIte for Universal App is installed from VS Tools / Extensions and Updates. Once installed it is referenced under Universal Windows / Extensions along with the Visual C++ 2015 which is also required.
Good luck!

Related

Trying to call x86 .Net 3.5 SDK in more recent .Net versions

This may be a long shot... I need to create a simple-ish web app that needs to call an OEM supplied SDK that is 10-15, or more, years old (no updates are possible). The SDK is 32bit and is pegged to .Net Framework 3.5. There is still an active NDA for using the SDK so there is not much that I can share.
I have access to Visual Studio 2015, 2017 and 2019. In each version I can go through the process of creating a C# web app and I can select .Net 3.5 as the target framework but the only available template is a "Blank" one. Are there MVC based templates for 3.5? I have googled and have not found any.
That is my first problem. I decided to just move on and try newer .Net versions that might have a more complete template set. I have tried 4.8 most recently.
Using .Net 4.8 and specifying an "x86" platform I can create a simple app. I can call the SDK lib and get values from the simple library "get" methods like "getHeight", "getWidth", etc. The SDK is for an old computer controlled device that still works like a charm. It has a camera and one of the available functions is GetCameraImage.
The SDK comes with documentation and some sample C# "SLN"s. One of those has the following sample code:
var idata = new byte[numOfBytes];
unsafe {
fixed (byte* fixedPtr = idata) {
channel.GetCameraImage((int) numOfBytes, out idata[0]);
}
}
In my code the above just crashes with an "Access Violation". No exceptions are thrown in the debugger. It just dies.
The docs that come with the SDK suggest that the following is valid:
var idata = new byte[numOfBytes];
channel.GetCameraImage((int) numOfBytes, out idata[0]);
but I get the same Access Violation.
Any ideas about the Access Violation?
EDIT
#Serg: Addressing a couple of comments: var idata = new byte[numOfBytes*3] did not work but was a great idea.
#DekuDesu: I am not sure how to exactly verify that the memory is allocated so I did the following:
var idata = new byte[numOfBytes];
for (var i=0;i<numOfBytes;i++) {
idata[1] += 0;
}
channel.GetCameraImage((int) numOfBytes, out idata[0]);
I also tried this with #Serg's offset idea and the violation is definately triggered by the GetCameraImage call.

Is calling System.Diagnostics.Process.Start() truly problematic?

In the Validation Result that I get when readying my app for submission to the Microsoft Store (which my app "PASSED WITH WARNINGS"), I got this "Fail" under the "Package Sanity Test" section:
The code which contains such a call is:
private void myMapsHelpToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://ramblingnotesofageezer.substack.com/p/map-o-matic-overview");
}
I am using that code due to the recommendation to do so here. How do I start a process from C#?
Is this truly a problem? If so, what should I use instead of the call to System.Diagnostics.Process.Start()?
I find it odd that it is classified as a failed part of the test, yet the overall results are that my app passed (albeit with warnings).
UPDATE
I checked out the link in the comment from Codexer, where it says, "Starting a utility can often provide a convenient way to obtain information from the operating system, access the registry, or access system capabilities. However, you can use UWP APIs to accomplish these sorts of tasks instead."
If this is the solution, do the UWP APIs have an equivalent to System.Diagnostics.Process.Start()?
UPDATE 2
I followed the steps in the answer, but the solution still doesn't compile, due to an error with this line of code:
await Windows.System.Launcher.LaunchUriAsync(uri);
In fact, even when I comment out the offending line, the project will no longer compile, but doesn't give me any information about how to solve the problem, just this:
I set the Package Management Format to PackageReference, and installed version 10.0.18362.2005 of Microsoft.Windows.SDK.Contracts, but it is complaining about needing a package reference...?!? I tried adding using Windows.System;
and using Microsoft.Windows.SDK.Contracts; but neither is recognized.
The package is installed for the project, as you can see here:
UPDATE 3
Regarding the "Must Use Package Reference" err msg, I have three questions revolving around what I see here:
The verbiage below Microsoft.Windows.SDK.Contracts says I can update versions of this package - should I?
I do not see a Reference to Microsoft.Windows.SDK.Contracts in my project's References, although it has been installed. Do I need to add one - if so, from where?
The context menu on my References affords me the ability to "Migrate packages.config to PackageReference..." should I do that?
If you were developing an uwp application, for opening open a web uri, it is recommended to use Launcher.LaunchUriAsync(Uri) method instead, this method starts the default browser to open the specified URI.
For example:
private async void button_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("http://www.google.com");
await Windows.System.Launcher.LaunchUriAsync(uri);
}
Update:
As Codexer mentioned, you could refer to this document to use uwp api in winform app.
I have created a winform project, its target framework is .Net Framework4.8. For earlier versions of .NET(.NET Core 3.x, .NET 5 Preview 7 (or earlier), or .NET Framework), you could refer to my steps.
1.Click Tools->NuGet Package Manager-> Package Manager Settings-> Change Default package management format to PackageReference. As follows:
2.Install the Microsoft.Windows.SDK.Contracts package, note that you need to install the appropriate version. Please check the corresponding version below. (Installation details: Right click Reference-> Manage NuGet Packages->Browse->Search Microsoft.Windows.SDK.Contracts->install)
10.0.19041.xxxx: Choose this for Windows 10, version 2004, version 20H2
10.0.18362.xxxx: Choose this for Windows 10, version 1903.
10.0.17763.xxxx: Choose this for Windows 10, version 1809.
10.0.17134.xxxx: Choose this for Windows 10, version 1803.
3.Run the project

Unable to find an entry point named 'sk_color_get_bit_shift' in DLL 'libSkiaSharp'. when using SkiaSharp 15.9.1

I am attempting to build and use the MicoCharts project available here: https://github.com/dotnet-ad/Microcharts which is dependant on this SkiaSharp project available here: https://github.com/mono/SkiaSharp
The specific version I am attempting to use is 15.9.1 (the version that the nuget package downloads) which utilizes skia m59.
I need to build them myself and cannot use Nuget due to business restrictions, just use the package isn't an option for me.
I have built skia m59, SkiaSharp and MicroCharts but when I attempt to create a SKBitmap object I get an error when it attempts to initialize SkiaSharp.SKImageInfo. The error is as follows:
Unable to find an entry point named 'sk_color_get_bit_shift' in DLL 'libSkiaSharp'.
I had to make a few changes to the base BUILD.gn to point to the correct file locations, for the windows SDK and the VC install. I enabled skia_use_gdi in the BUILD.gn and ran the following commands.
python2 tools/git-sync-deps
gn gen out/Release --args="is_debug=false is_official_build=true skia_use_system_expat=false skia_use_system_libjpeg_turbo=false skia_use_system_libpng=false skia_use_system_libwebp=false skia_use_system_zlib=false skia_use_icu=false is_component_build=true"
ninja -C out/Release skia
This process outputs a DLL I assumed is the same as the libSkiaSharp that the SkiaSharp project relies on. I add all my references and run, the project runs successfully until I attempt to create the SkBitmap object then it fails.
Either this DLL is not the correct DLL and I am misunderstanding something here or something in my process is wrong. I would love any help I can get as I am completely new to building these sorts of projects, I am a C# developer by trade.
This is not the same thing. SkiaSharp has a few other bits that it adds to the core skia. The output that you would have got is a skia.dll, which only part. Not sure how you got a libSkiaSharp from the skia target...
If you can't use SkiaSharp from NuGet.org (which is the supported case) you can follow this to build your own: https://github.com/mono/SkiaSharp/wiki/Building-SkiaSharp
You can also check out the Azure DevOps yaml: https://github.com/mono/SkiaSharp/blob/master/scripts/azure-pipelines.yml
Just set up your own DevOps job to use that and all the work will be done for you.

SQLite removing reference on rebuild

SQL Lite states that its added support for Xamrian Forms and .net standard and yet when I rebuild my project all references disappear what it the correct library I need to get SQL lite working in .net standard 2.0 I am using it to sync between a mobile app and the server.
public async Task SyncAllDeliverys()
{
Task<string> callTask = Task.Run(() => GetDeliverysFromAPi());
// Wait for it to finish
callTask.Wait();
// Get the result
string content = callTask.Result;
//Sends a GET request to the specified Uri and returns the response body as a string in an asynchronous operation
deliverysItems = JsonConvert.DeserializeObject<List<DeliverysItems>>(content); //Deserializes or converts JSON String into a collection of Post
SQLiteAsyncConnection conn = new SQLiteAsyncConnection(dbPath);
await conn.InsertAllAsync(deliverysItems);
}
As you see here my project is .net standard 2.0
Edit 2
To show the correct version that I have installed instead of the incorrect one I showed in the first graphic.
Edit 2
It would appear that this is a known bug and xamrian has submitted it as such in 2019 visual studio.
https://github.com/xamarin/Xamarin.Forms/issues/5983
You are looking at the wrong description of dependencies. The dependencies in the first screenshot(you circled in the right bottom) belongs to sqlite-net-pcl instead of SQLite.Net-PCL.
Package 'SQLite.Net-PCL 3.1.1' was restored using
'.NETFramework,Version=v4.6.1'.
You can try uninstall and install sqlite-net-pcl nuget, clean and rebuild your project.
what it the correct library I need to get SQL lite working in .net
standard 2.0
Use sqlite-net-pcl is the right way. (The fourth library in your screenshot).
There are a number of NuGet packages with similar names, the correct package has these attributes:
Created by: Frank A. Krueger
Id: sqlite-net-pcl
NuGet link: sqlite-net-pcl
Refer: databases

Ninject Portable in Xamarin results in NotImplementedException

When I use Portable.Ninject in a Xamarin.Forms app, instantiating the StandardKernel always results in a NotImplementedException.
I can consistently replicate the problem as follows:
Create Xamarin.Forms application (in VS2017: Cross Platform App (Xamarin))
Configure it to use PCL
I'm only interested in Android and for the sake of brevity, I removed all other platform projects.
Add NuGet Package Portable.Ninject to the PCL and Android platforms
Then, in App.xaml.cs I simply try the following:
public App()
{
InitializeComponent();
//ommitting NinjectModules for brevity
var kernel = new Ninject.StandardKernel(); //exception is thrown here
MainPage = new NavigationPage(new MainView());
}
What am I missing here?
Currently I'm using Portable.Ninject version 3.3.1.
I also tried the XLabs.IoC.Ninject package (which also uses Portable.Ninject) and got the same result.
For those who experience the same problem:
tldr; clean you projects, and rebuild.
Explanation: At first I had only added Ninject to my PCL, and had forgotten to add it to the Android project. Although I added the Ninject library to the Android project soon afterwards, it would still result in the exception being thrown.
The solution was to clean the projects and rebuild them. Sometimes it's just that easy!

Categories