-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindowViewModel.cs
55 lines (44 loc) · 1.41 KB
/
MainWindowViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MongoDB.Bson;
using Realms;
using Realms.Exceptions;
namespace RealmWinUI
{
public partial class MainWindowViewModel : ObservableObject
{
public MainWindowViewModel()
{
var config = new RealmConfiguration(@"d:\temp\cats.realm");
_localRealm = Realm.GetInstance(config);
CatsQuery = _localRealm.All<Cat>(); //.AsQueryable();
}
private Realm _localRealm;
[ObservableProperty]
private IQueryable<Cat> _catsQuery;
public Cat? SelectedCat { get; set; }
[RelayCommand]
private void RemoveCat()
{
if (SelectedCat == null)
return;
_localRealm.Write(() => { _localRealm.Remove(SelectedCat); });
}
[RelayCommand]
public void Populate()
{
Cat ninja = new() { Name = "Ninja", Age = 1, Breed = "Angora" };
Cat nounou = new() { Name = "Nounou", Age = 2, Breed = "Siamese" };
_localRealm.Write(() => _localRealm.Add(nounou));
_localRealm.Write(() => _localRealm.Add(ninja));
}
}
}