I made a simple Xamarin cross-platform sqlite application for Android and IOS. I want to add Universal Windows Phone version too. To do that i followed this steps. I installed SQLite.Net-PCL from nuget manager. But i still need to write something for sqlite connection. In this link, at step 6 there is code for this but its for windows phone 8. Can you please help me?
Xamarin.Forms supports database-driven applications using the SQLite database engine, which makes it possible to load and save objects in shared code.
Xamarin.Forms applications can use the SQLite.NET PCL NuGet package to incorporate database operations into shared code by referencing the SQLite classes that ship in the NuGet. Database operations can be defined in the Portable Class Library (PCL) project of the Xamarin.Forms solution, with platform-specific projects returning a path to where the database will be stored.
But i still need to write something for sqlite connection. In this link, at step 6 there is code for this but its for windows phone 8. Can you please help me?
Windows 10 Universal Windows Platform (UWP)
Add the SQLite-net PCL NuGet to the UWP project by right-clicking and choosing Manage NuGet Packages.
Once the reference is added, implement the IFileHelper interface using the platform-specific Windows.Storage API to determine the data file path.
using Windows.Storage;
...
[assembly: Dependency(typeof(FileHelper))]
namespace Todo.UWP
{
public class FileHelper : IFileHelper
{
public string GetLocalFilePath(string filename)
{
return Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
}
}
}
Details please reference Xamarin.Forms Local Databases. The accompanying sample application is a simple Todo-list application.
Related
I am working on a UWP app and I can't find the async API mentioned here.
https://github.com/praeclarum/sqlite-net#asynchronous-api
I have installed only the sqlite-net-pcl 1.5.231 nuget. (https://www.nuget.org/packages/sqlite-net-pcl)
Am I missing anything?
With my tests, there's no problem. I can call the async APIs. Please check if you add the SQLite.cs and SQLiteAsync.cs to your project like the Source Installation mentioned.
By the way, you could follow the official document Use a SQLite database in a UWP app to use sqlite database in your UWP app.
How can I check if Internet is available in Xamarin forms shared project.
We can use Cross connectivity plugin in PCL project,
can we use the same plugin in shared project as well?
The Cross Connectivity Plugin has been deprecated in favor of Xamarin.Essentials.
First, add the Xamarin.Essentials NuGet Package to both the iOS project and Android project.
Then use it to check connectivity in the shared project:
var current = Connectivity.NetworkAccess;
if (current == NetworkAccess.Internet)
{
// Connection to internet is available
}
I created a Property which returns CrossConnectivity.Current.IsConnected in its getter.
Also using the Plugin.Connectivity.CrossConnectivity NuGet Package.
I've created a Xamarin.Forms project having Android, iOS and Windows Phone. I'm trying to use the Sqlite Database in Xamarin Windows Phone 8.1 project and I've installed the sqlite.net pcl and core and async libraries with platform library. When I try to run the project it shows an error winrt.dll sqlite3 could not be found. There are actually two reference libraries for sqlite.platforms.windowsPhone8 so I've added it as x86 but still having problem.
Here is my code for windows phone 8.1
namespace SwachhParyatanApp.WinPhone
{
class DBPath_WinPhone : IDBPath
{
public SQLiteAsyncConnection GetDBPath()
{
var path = Path.Combine(ApplicationData.Current.LocalFolder.Path, "localData.db");
var platform = new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT();
var param = new SQLiteConnectionString(path, false);
var connection = new SQLiteAsyncConnection(() => new SQLiteConnectionWithLock(platform, param));
return connection;
}
}
}
Whenever I try to call using the dependency service its shows error on the line var platform = new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT();
By default the WinRT library was added to the project but it was also having problem so I added the windowsphone8 library.
Windows phone does not included the SQLite, like iOS and Android.
You need to download the SQLite extension for VS, and reference it in your project.
I would recommend using the SQLite-net PCL, it's an ORM for SQLite and provides nice async/synchronous APIs on top of regular SQLite APIs
I've found the answer, we need to add sqlite3.dll from sqlite.org for windows phone 8.1 WinRT site manually.
I used Visual Studio 2015 with SP2. I try created Windows Universal app with sqlite. I added SQLite for Unversal Windows Platforms and SQLite.Net-PCl. It is my simple code
var conn = new SQLiteConnection(
new SQLitePlatformWinRT(),
Path.Combine(ApplicationData.Current.LocalFolder.Path, "Storage.sqlite"));
It is working for desktop. But it is not work for mobile(in device and emulator). I get this exception System.DllNotFoundException:
HResult=-2146233052
Message=Unable to load DLL 'sqlite3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Source=SQLite.Net.Platform.WinRT
TypeName=""
StackTrace:
at SQLite.Net.Platform.WinRT.SQLite3.SetDirectory(UInt32 directoryType, String directoryPath)
at SQLite.Net.Platform.WinRT.SQLiteApiWinRT..ctor()
at SQLite.Net.Platform.WinRT.SQLitePlatformWinRT..ctor()
at AppDbTest.MainPage.DbConnection()
at AppDbTest.MainPage..ctor()
at AppDbTest.AppDbTest_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_MainPage()
at AppDbTest.AppDbTest_XamlTypeInfo.XamlUserType.ActivateInstance()
I try this method. I reinstall SQLite for Unversal Windows Platforms extension and SQLite.Net-PCl. I created clear project. But it does not work for me.
I add a reference to Visual C++ 2015 Runtime for Universal Windows Platform Apps. It is work for me. I used SQLite for Universal Windows Platform.
Make sure that you current add 2 references
Sqlite for Universal Windows Platfrom
Visual C++ Runtime 2015 for Unversal Windows Platfrom
It's still worked!
Solved in chat.
OP solved it using Nuget console :
Install-Package SQLite.WinRT.UAP
and used "add link" ->"Extensions"->SQLite for Windows Runtime
Suggestion
Cool but would suggest you to use Extension SQLite for Universal App Platform as it encases most of device family and SQLite.WinRT.UAP is not needed.
As in current repo I removed references for Winrt and using these extensions its working fine. Just check if the Sqlite vsix package installed is the correct one on your system.
Can I have a .NET Class Library project with Entity Framework 6.1.1 (in windowsazure.mobileservices.backend.entity nuget package) work with Xamarin.Forms PCL app? Is this achievable? If not what are the possible complications?
Question Background:
I am working on a Xamarin.Forms cross platform PCL app with Azure Mobile Services back-end, targeting Android, iOS, Windows 8.1 and WinPhone 8.1. I actually dont care about Windows 8.1 but Visual Studio does not allow me to remove this target. So it stays.
Azure Mobile Services is setup, but I need the tables to be generated automatically (strongly avoiding manually typing table definitions in azure portal). I decided to go EF code first way with windowsazure.mobileservices.backend.entity nuget package to achieve this. However, the Xamarin.Forms PCL project does not support additional assemblies.
I am thinking of adding this package into a separate class library project and referring to it from the PCL project.
Thanks for your help!
The EntityData type is the required superclass of DTOs on the server SDK. Unfortunately, it has dependencies that are not supported in a PCL project, so it has to be used with a full .NET 4.5 project.
You could instead use partial classes to share DTOs between client and server. Just put all of the non system property types in a common code file that's shared between client and server. Then, on the client, add the system property implementation (ID field, UpdatedAt optionally, Version optionally). On the server, add another partial class definition that inherits from EntityData.