Xamarin with SQLite Telerik School Academy Xamarin apps for iOS, Android & WinPhone
1. Architecture 2. Install SQLite.NET – for PCL Project 3. Implementation Shared code Android iOS Windows Phone 2
3
4
PCL – Portable Class Library Install SQLite.Net PCL from NuGet 5
6
Initial database setup requires platform-specific work Use Interfaces to abstract any platform-specific functionality Use the DependencyService to obtain an implementation public interface ISQLite { SQLiteConnection GetConnection(); SQLiteConnection GetConnection();} 7 public ItemDatabase() { SQLiteConnection database = DependencyService SQLiteConnection database = DependencyService.Get<ISQLite>().GetConnection(); InitializeDatabase(database); InitializeDatabase(database);}
Create Table Read item/s from database Create/Add item to the database Delete item from the database Updata item in the database database.CreateTable<T>(); 8 database.Table<T>(); database.Insert(item); database.Delete<T>(item); database.Update(item);
9
Add SQLite.PCL – XamarinAndroid Platform from NuGet Database location var sqliteFilename = "SQLite.db3"; string docPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal); (System.Environment.SpecialFolder.Personal); var path = Path.Combine(docPath, sqliteFilename); 10
Implement ISQLite Add assembly Dependency 11 [assembly: Dependency (typeof (SQLite_Android))] public class SQLite_Android : ISQLite { public SQLiteConnection GetConnection () { public SQLiteConnection GetConnection () { var path =...; var path =...; var connection = new SQLiteConnection( var connection = new SQLiteConnection( new SQLitePlatformAndroid(), path); new SQLitePlatformAndroid(), path); return connection; return connection; }}
12
Add SQLite.PCL – XamarinIOS Platform from NuGet Database location var sqliteFilename = "SQLite.db3"; string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); string libraryPath = Path.Combine (documentsPath, "..", "Library"); var path = Path.Combine(libraryPath, sqliteFilename); 13
Implement ISQLite Add assembly Dependency [assembly: Dependency (typeof (SQLite_iOS))] public class SQLite_iOS : ISQLite { public SQLiteConnection GetConnection () { public SQLiteConnection GetConnection () { var path =...; var path =...; var connection = new SQLiteConnection( var connection = new SQLiteConnection( new SQLitePlatformIOS(), path); new SQLitePlatformIOS(), path); return connection; return connection; }} 14
15
Windows Phone does not include the SQLite database engine Download and Install Precompiled Binaries for Windows Phone 8 Precompiled Binaries for Windows Phone 8Precompiled Binaries for Windows Phone 8 Add References Windows Phone SDK 8.0 > Extentions > SQLite 16
Add SQLite.PCL – WindowsPhone8 Platform from NuGet Implement ISQLite var sqliteFilename = "SQLite.db3"; string path = Path.Combine( ApplicationData.Current.LocalFolder.Path, ApplicationData.Current.LocalFolder.Path, sqliteFilename); sqliteFilename); 17
Implement ISQLite Add assembly Dependency [assembly: Dependency (typeof (SQLite_WinPhone))] public class SQLite_WinPhone : ISQLite { public SQLiteConnection GetConnection () { public SQLiteConnection GetConnection () { var path =...; var path =...; var connection = new SQLiteConnection( var connection = new SQLiteConnection( new SQLitePlatformWP8(), path); new SQLitePlatformWP8(), path); return connection; return connection; }} 18
Live Demo 19
Architecture Install SQLite.NET For PCL Project Implementation Shared code Android iOS Windows Phone 20
форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране
C# Telerik Academy csharpfundamentals.telerik.com csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com academy.telerik.com Telerik Facebook facebook.com/TelerikAcademy facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com forums.academy.telerik.com