5
5
using Microsoft . UI . Xaml ;
6
6
using Microsoft . UI . Xaml . Controls ;
7
7
using LiteDB ;
8
+ using Microsoft . UI . Xaml . Navigation ;
8
9
9
10
namespace LiteDBSample
10
11
{
@@ -21,7 +22,20 @@ public MainPage()
21
22
_todoItems = new ObservableCollection < TodoItem > ( ) ;
22
23
}
23
24
24
- private LiteDatabase _liteDatabase ;
25
+ protected override async void OnNavigatedTo ( NavigationEventArgs e )
26
+ {
27
+ base . OnNavigatedTo ( e ) ;
28
+
29
+ // Before the database is accessed for the first time on WASM, the filesystem needs to be
30
+ // initialized, otherwise data might be lost. Ensure that any asynchronous operation is
31
+ // awaited before creating/accessing the database, as await will wait for the initialization to complete.
32
+ // See https://platform.uno/docs/articles/features/file-management.html#webassembly-file-system for more information.
33
+ var localFolder = Windows . Storage . ApplicationData . Current . LocalFolder ;
34
+ var data = await localFolder . CreateFolderAsync ( "Data" , CreationCollisionOption . OpenIfExists ) ;
35
+ LoadFromDatabase ( this , null ) ;
36
+ }
37
+
38
+ private LiteDatabase _liteDatabase ;
25
39
26
40
private LiteDatabase LiteDatabase
27
41
{
@@ -36,7 +50,7 @@ private LiteDatabase LiteDatabase
36
50
}
37
51
}
38
52
39
- private static string DbPath => System . IO . Path . Combine ( ApplicationData . Current . LocalFolder . Path , "save.db" ) ;
53
+ private static string DbPath => System . IO . Path . Combine ( ApplicationData . Current . LocalFolder . Path , "Data" , " save.db") ;
40
54
41
55
private void LoadFromDatabase ( object sender , RoutedEventArgs args )
42
56
{
0 commit comments