/
SaveToJsonXaml.xaml.cs
67 lines (62 loc) · 2.13 KB
/
SaveToJsonXaml.xaml.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
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;
using Xamarin.Forms;
using XF_DataSaveSample.ViewModels;
namespace XF_DataSaveSample.Views
{
public partial class SaveToJsonXaml : ContentPage
{
private AllPagesViewModel vm = new AllPagesViewModel();
public SaveToJsonXaml()
{
InitializeComponent();
this.BindingContext = vm;
}
async void clearButton_Clicked(object sender, EventArgs e)
{
if (DependencyService.Get<ISaveAndLoad>().ClearData("temp.json"))
{
resultLabel.Text = null;
await DisplayAlert("Done", "Data is cleared", "OK");
}
else
{
await DisplayAlert("Error", "Data cannot be cleared", "OK");
}
}
async void loadButton_Clicked(object sender, EventArgs e)
{
var data = DependencyService.Get<ISaveAndLoad>().LoadData("temp.json");
if (data != null)
{
this.vm = JsonConvert.DeserializeObject<AllPagesViewModel>(data);
this.BindingContext = vm;
resultLabel.Text = string.Format("Name: {0}\nBirthday: {1:yyyy/MM/dd}\nLike?: {2}", vm.Name, vm.Birthday, vm.Like);
}
else
{
await DisplayAlert("Error", "Data is not saved", "OK");
}
}
async void saveButton_Clicked(object sender, EventArgs e)
{
if (vm.Name != null)
{
var json = JsonConvert.SerializeObject(vm);
DependencyService.Get<ISaveAndLoad>().SaveData("temp.json", json);
await DisplayAlert("Data Saved", string.Format("Name: {0}\nBirthday: {1:yyyy/MM/dd}\nLike?: {2}", vm.Name, vm.Birthday, vm.Like), "OK");
resultLabel.Text = string.Empty;
}
else
{
await DisplayAlert("Error", "Please input your name.", "OK");
}
}
}
}