-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently this works for Android only and not for iOS. See dotnet/efcore#7158 for details
- Loading branch information
1 parent
9c50590
commit f99e22e
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
XamarinLocalDataAccessDemo/XamarinLocalDataAccessDemo/Models/DishDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace XamarinLocalDataAccessDemo.Models | ||
{ | ||
public class DishDbContext : DbContext | ||
{ | ||
private readonly string _databasePath; | ||
|
||
public DishDbContext(string databasePath) | ||
{ | ||
_databasePath = Path.Combine(databasePath, "dishes.db"); | ||
} | ||
public DbSet<Dish> Dishes { get; set; } | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseSqlite($"Filename={_databasePath}"); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
XamarinLocalDataAccessDemo/XamarinLocalDataAccessDemo/Pages/EfCoreDemoPage.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
x:Class="XamarinLocalDataAccessDemo.Pages.EfCoreDemoPage" | ||
Title="ListView"> | ||
<ListView ItemsSource="{Binding Items}" | ||
IsPullToRefreshEnabled="true" | ||
CachingStrategy="RecycleElement" | ||
IsRefreshing="{Binding IsBusy, Mode=OneWay}" | ||
RefreshCommand="{Binding RefreshDataCommand}"> | ||
<ListView.Header> | ||
<StackLayout Padding="10" | ||
Orientation="Horizontal" | ||
HorizontalOptions="FillAndExpand" | ||
BackgroundColor="#dadada"> | ||
<Button Text="Add Entry" Command="{Binding AddCommand}"></Button> | ||
</StackLayout> | ||
</ListView.Header> | ||
<!--Built in Cells--> | ||
<ListView.ItemTemplate> | ||
<DataTemplate> | ||
<TextCell Text="{Binding Name}" | ||
Detail="{Binding Description}"/> | ||
</DataTemplate> | ||
</ListView.ItemTemplate> | ||
|
||
|
||
</ListView> | ||
</ContentPage> |
102 changes: 102 additions & 0 deletions
102
XamarinLocalDataAccessDemo/XamarinLocalDataAccessDemo/Pages/EfCoreDemoPage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Input; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Xaml; | ||
using XamarinLocalDataAccessDemo.Models; | ||
using XamarinLocalDataAccessDemo.Services; | ||
|
||
namespace XamarinLocalDataAccessDemo.Pages | ||
{ | ||
[XamlCompilation(XamlCompilationOptions.Compile)] | ||
public partial class EfCoreDemoPage : ContentPage | ||
{ | ||
public EfCoreDemoPage() | ||
{ | ||
InitializeComponent (); | ||
BindingContext = new EfCoreDemoPageViewModel(); | ||
} | ||
|
||
} | ||
|
||
class EfCoreDemoPageViewModel : INotifyPropertyChanged | ||
{ | ||
public ObservableCollection<Dish> Items { get; } | ||
bool _busy; | ||
private DishDbContext _db; | ||
|
||
public EfCoreDemoPageViewModel() | ||
{ | ||
|
||
var path = DependencyService.Get<IPathService>().GetDatabasePath(); | ||
_db = new DishDbContext(path); | ||
_db.Database.EnsureCreated(); | ||
RefreshDataCommand = new Command( | ||
async () => await RefreshData()); | ||
AddCommand = new Command( | ||
async () => await AddData()); | ||
|
||
Items = new ObservableCollection<Dish>(_db.Dishes.ToList()); | ||
} | ||
|
||
public ICommand RefreshDataCommand { get; } | ||
public ICommand AddCommand { get; } | ||
|
||
async Task RefreshData() | ||
{ | ||
IsBusy = true; | ||
IsBusy = true; | ||
|
||
Items.Clear(); | ||
|
||
foreach (var dish in _db.Dishes) | ||
{ | ||
Items.Add(dish); | ||
} | ||
IsBusy = false; | ||
await Task.Delay(2000); | ||
|
||
IsBusy = false; | ||
} | ||
|
||
async Task AddData() | ||
{ | ||
IsBusy = true; | ||
var dish = new Dish | ||
{ | ||
Name = $"Ensalada Fantasia ({DateTime.Now:T})", | ||
Description = "Salat aus allen Zutaten, die noch übrig waren", | ||
Price = 1.99, | ||
CategoryId = 1 | ||
}; | ||
await _db.Dishes.AddAsync(dish); | ||
await _db.SaveChangesAsync(); | ||
IsBusy = false; | ||
} | ||
|
||
public bool IsBusy | ||
{ | ||
get { return _busy; } | ||
set | ||
{ | ||
_busy = value; | ||
OnPropertyChanged(); | ||
((Command)RefreshDataCommand).ChangeCanExecute(); | ||
} | ||
} | ||
|
||
|
||
public event PropertyChangedEventHandler PropertyChanged; | ||
void OnPropertyChanged([CallerMemberName]string propertyName = "") => | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters