There are many ways to do it, this is my preferred way, since you have not put any code (you should do it in the future to make things easier for those of us who want to help you) I have created this small solution.
You should add this Nuget package (there are many SQLite options) - SQLite.Net-PCL
And for convenience I recommend adding this VS extension
sqlite-uap-3100000.vsix : VSIX package for Universal App Platform development using Visual Studio 2015
public class User : BindableBase
{
private int _id;
public int Id
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
private string _name;
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value); }
}
}
view model
I have chosen to do lazy initialization of the viewmodel, so that I can instantiate the viewmodel from xaml and still make sure the viewmodel is initialized. The Initialize method handles initialization (a bit of an obvious one). To save you trouble I have additionally used a deadlock to avoid concurrency errors.
public class MainAppVM : BindableBase
{
private bool _initialized = false;
private object _lockject = new object();
private User _firstUser;
public User FirstUser
{
get
{
Initialize();
return _firstUser;
}
set
{SetProperty(ref _firstUser, value);}
}
private List<User> _userList;
public List<User> UserList
{
get
{
Initialize();
return _userList;
}
set
{SetProperty(ref _userList, value);}
}
public void Initialize()
{
lock (_lockject)
{
if (!_initialized)
{
_initialized = true;
var path = Path.Combine(ApplicationData.Current.LocalFolder.Path, "db.sqlite");
using (SQLiteConnection conn = new SQLiteConnection(new SQLitePlatformWinRT(), path))
{
var list = from user in conn.Table<User>()
select user;
this._firstUser = list.FirstOrDefault();
this._userList = list.ToList();
}
}
}
}
}
xaml
look at line #6 that starts with xmlns:vmthere I have referenced the namespace where I have the viewmodel.
There are many ways to do it, this is my preferred way, since you have not put any code (you should do it in the future to make things easier for those of us who want to help you) I have created this small solution.
You should add this Nuget package (there are many SQLite options) - SQLite.Net-PCL
And for convenience I recommend adding this VS extension
model
You can Get BindableBase here if you don't have it yet
view model
I have chosen to do lazy initialization of the viewmodel, so that I can instantiate the viewmodel from xaml and still make sure the viewmodel is initialized. The Initialize method handles initialization (a bit of an obvious one). To save you trouble I have additionally used a deadlock to avoid concurrency errors.
xaml
look at line #6 that starts with
xmlns:vm
there I have referenced the namespace where I have the viewmodel.Then I instantiate the viewmodel as datacontext
And then I solve everything with the binding.
And ready! that's all, in my case I have added something in the MainPage.xaml.cs to seed test data after each execution:
Download here the complete example