Everyone.
I am using SqLite in Xamarin Forms. For that I am using SqLite - PCL nuget. Now the problem is, It works fine in Android. But in UWP I can't Access database file.
I am using Common code for both Platforms. It is as below :
database = new SQLiteConnection(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "mydb.db"));
database.CreateTable<Item>();
The error I am getting in UWP app only.
Thank You.
the docs contain clear instructions on how to do this in UWP
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);
}
}
}
Related
I am trying to create a plugin using .Net Maui class libraries that use native Android and iOS code. I tried using the Dependency Service using the Interface, but I get a null point when I try to Register the Interface.
Shared code
public class MyPlugin
{
public static DisplayModel()
{
IDeviceOrientationService service =
DependencyService.Get<IMyPlugin>();
var model = service.GetDeviceName();
}
}
public Interface IMyPlugin
{
string GetDeviceName();
}
Android platform
[assembly: Dependency(typeof(MyPlugin))]
public class MyPlugin:IMyPlugin
{
public string GetDeviceName
{
return "From Android";
}
}
This is just an example. Please help me understand if I am not going about this the correct way and any suggestion would be appreciated.
I have a page where I display all sorts of info and I would like to display either the android Sdk used or the iOS version used
How can I retrieve those in xamarin forms? Looked at xamarin essentials but cannot find it.
Result wanted
Android Sdk
29 (10)
ioS
14
Hope makes sense
thanks
You can get via Dependency Service. Create Interface in shared project and then implement this interface in platform specific projects.
Shared Project
public interface IDeviceInfo
{
string GetOSVersion();
}
iOS Project
[assembly: Dependency(typeof(DeviceInfo_iOS))]
namespace App1.iOS
{
public class DeviceInfo_iOS : IDeviceInfo
{
public string GetOSVersion()
{
return UIDevice.CurrentDevice.SystemVersion;
}
}
}
Android Project
[assembly: Dependency(typeof(DeviceInfo_Android))]
namespace App1.Droid
{
public class DeviceInfo_Android : IDeviceInfo
{
public string GetOSVersion()
{
return "Android Sdk " + (int)Build.VERSION.SdkInt +" (" + Build.VERSION.Release +")";
}
}
}
Call the DependencyService in Shared Project
var version = DependencyService.Get<IDeviceInfo>().GetOSVersion();
I have a Xamarin Forms application that works fine on Android, but when I run on iOS, I get this error when I set up the database
Method not found: string SQLitePCL.raw.sqlite3_column_name(SQLitePCL.sqlite3_stmt,int)
and then when I try to insert into the database table
Method not found: string SQLitePCL.raw.sqlite3_errmsg(SQLitePCL.sqlite3)
The application uses mvvmlight and I have the sqlite-net-pcl package installed there and also in the platform code. The versions all marry up.
My iOS initialising code for sqlite looks like this (on the platform)
public class SQLiteConnectionFactory : ISqliteConnectionFactory
{
readonly string Filename = "mydb";
public SQLiteConnection GetConnection()
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
path = Path.Combine(path, Filename);
SimpleIoc.Default.Register<ISqliteConnectionFactory, SQLiteConnectionFactory>();
return new SQLiteConnection(path, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.SharedCache);
}
and the insert code (in the mvvmlight project) like this
public void SaveListData<T>(List<T> toStore)
{
try
{
lock (dbLock)
{
connection.InsertAll(toStore);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
I've had issues with Xam.Android and sqlite-net-pcl, but never on iOS
I have the sqlite-net-pcl 1.7.302-beta and SQLitePCL.* 2.0.2 packages installed (using the beta version to prevent some of the linking issues on the stable version)
Below is a shot of the iOS packages installed
Downgrade your sqlite-net-pcl package to version 1.5.231, that did the job for me
I can't seem to get off dead center with SQLite.Net-PCL; I have a Xamarin forms solution with a PCL proj, Droid proj, iOS proj, and Windows Universal project. I have installed the nuget packages for SQLite.Net-PCL and SQLite.Net.Core-PCL.
So, I go over to the github project to find some awesome examples to get started and none of them work; apparently you have to pass a platform into the database connection, but I get a reference error on them all (such as SQLitePlatformWin32).
Search the web for the references and... nothing. Search nuget for the platforms and... nothing.
What am I missing? (yes, I feel dumb)
An example they have is
public class Database : SQLiteConnection
{
public Database(string path) : base(new SQLitePlatformWin32(), path)
{
CreateTable<Stock>();
CreateTable<Valuation>();
}}
and I get a reference error that I can't resolve on the "new SQLitePlatformWin32" line.
For anyone else struggling, here is what you need to do.
Install the SQLite.Net-PCL, SQLite.Net.Core-PCL and SQLite.Net.Async-PCL nugets
Create an interface for ISQLite in your PCL:
public interface ISQLite
{
SQLiteConnection GetConnection();
}
Call GetConnection via DependencyService
PatientDatabase = DependencyService.Get<ISQLite>().GetConnection();
PatientDatabase.CreateTable<Patient>();
The above will create the connection based on your platform (i.e. android, ios). In each platform's project, you must have a GetConnection that is accessable via DependencyService such as this for Android
[assembly: Xamarin.Forms.Dependency(typeof(SQLite_Android))]
// ...
public class SQLite_Android : ISQLite
{
public SQLite_Android() { }
public SQLite.Net.SQLiteConnection GetConnection()
{
var sqliteFilename = "TodoSQLite.db3";
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder
var path = Path.Combine(documentsPath, sqliteFilename);
// Create the connection
var conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(), path);
// Return the database connection
return conn;
}
}
My problem was I was trying to create the connection in my PCL and I couldn't find the platforms, what you need to do is create the connection in each individual project.
I have a problem with this Nuget Package:
https://www.nuget.org/packages/sqlite-net-pcl/1.0.11
In my test Case I have a PCLLibrary called "PCL" with this NuGet Package. And an emtpy class which has
using SQLite;
namespace PCL
{
public class Test
{
}
}
Now I create a new Xamarin.Forms PCL Project and reference this Library
using System;
using Xamarin.Forms;
namespace PCL
{
public class App : Application
{
public App ()
{
// The root page of your application
MainPage = new ContentPage {
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
XAlign = TextAlignment.Center,
Text = "Welcome to Xamarin Forms!"
}
}
}
};
var test = new Test();
}
}
}
Now I run my App in iOS Simulator and it gets killed ...
When i am not creating an instance of Test this App starts fine...
When I integrate this Package directly in my Forms Library, I can use it normally.
What can cause this?
Normally when you are dealing with NuGet packages on PCL's you need to add the NuGet package on the iOS project as well as the PCL. This is because it may have some platform specific code in it (will definitely be the case with a SQLite library).
When you add it to the iOS project it loads the specific assemblies for iOS that is needed. Its just something you need to be aware of.
Also I would recommend this package: https://www.nuget.org/packages/SQLite.Net-PCL/