-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddWindow.xaml.cs
More file actions
89 lines (74 loc) · 2.2 KB
/
AddWindow.xaml.cs
File metadata and controls
89 lines (74 loc) · 2.2 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using Newtonsoft.Json;
using System.IO;
using System.Windows;
using System.Windows.Input;
namespace Mini_Store
{
public partial class AddWindow : Window
{
private List<MyItem> dataList = new List<MyItem>();
public AddWindow()
{
InitializeComponent();
LoadData();
}
public class MyItem
{
public Data? data { get; set; }
}
public class Data
{
public string? Name { get; set; }
public string? Icon { get; set; }
public string? Link { get; set; }
}
private void Border_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
this.DragMove();
}
}
private void Close_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var newData = new Data
{
Name = TextBoxName.Text,
Icon = TextBoxIcon.Text,
Link = TextBoxLink.Text
};
var newItem = new MyItem { data = newData };
dataList.Add(newItem);
SaveData();
Restart();
}
private void SaveData()
{
string jsonString = JsonConvert.SerializeObject(dataList, Formatting.Indented);
File.WriteAllText("Data.json", jsonString);
}
private void LoadData()
{
if (File.Exists("Data.json"))
{
string jsonString = File.ReadAllText("Data.json");
dataList = JsonConvert.DeserializeObject<List<MyItem>>(jsonString) ?? new List<MyItem>();
}
}
private void Restart()
{
var currentWindow = Application.Current.MainWindow;
currentWindow.Close();
var newWindow = new MainWindow();
newWindow.Show();
Application.Current.MainWindow = newWindow;
Close();
AddWindow addWindow = new AddWindow();
addWindow.ShowDialog();
}
}
}