This repository was archived by the owner on Feb 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewModel.cs
More file actions
175 lines (166 loc) · 3.61 KB
/
ViewModel.cs
File metadata and controls
175 lines (166 loc) · 3.61 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.Windows;
using System.IO;
using System.ServiceProcess;
namespace HTMLtoXAML
{
class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
FilePath = "C:/Users/lexaz/Desktop/test.html";
}
public Model model = new Model();
bool let = false;
public event EventHandler<EventArgs> OnFileUpdate;
public FileWatcher fw = new FileWatcher();
public Interpreter interpreter = new Interpreter();
private Uri webFilePath;
public Uri WebFilePath
{
get { return webFilePath; }
set
{
webFilePath = value;
OnPropertyChanged("WebFilePath");
}
}
private string filePath;
public string FilePath
{
get { return filePath;}
set
{
filePath = value;
WebFilePath = new Uri(String.Format("file:///" + value));
OnPropertyChanged("FilePath");
}
}
private DefaultDialogService dialogService = new DefaultDialogService();
private string bgcolor = "#FF00AB5E";
public string bgColor
{
get { return bgcolor; }
set
{
bgcolor = value;
OnPropertyChanged("bgColor");
}
}
private string btntext = "Start";
public string btnText
{
get { return btntext; }
set
{
btntext = value;
OnPropertyChanged("btnText");
}
}
private RelayCommand openFile;
public RelayCommand OpenFile
{
get
{
return openFile ??
(openFile = new RelayCommand(obj =>
{
try
{
if (dialogService.OpenFileDialog() == true)
{
FilePath = dialogService.FilePath;
}
}
catch (Exception ex)
{
dialogService.ShowMessage(ex.Message);
}
}));
}
}
public FileSystemWatcher watcher;
void funcChangedHandler(object sender, FileSystemEventArgs e)
{
if (let == false)
{
if (e.Name == Path.GetFileName(FilePath))
{
MessageBox.Show("File has updated!");
interpreter.ReadAndWrite(FilePath);
}
let = true;
}
else
{
let = false;
}
}
private RelayCommand startWatcher;
public RelayCommand StartWatcher
{
get
{
return startWatcher ??
(startWatcher = new RelayCommand(obj =>
{
if (watcher != null)
return;
bgColor = "#FFAB0000";
btnText = "Stop";
try
{
watcher = new FileSystemWatcher(Path.GetDirectoryName(filePath));
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(funcChangedHandler);
watcher.EnableRaisingEvents = true;
}
catch (Exception ex)
{
dialogService.ShowMessage(ex.Message);
}
}));
}
}
private RelayCommand stopWatcher;
public RelayCommand StopWatcher
{
get
{
return stopWatcher ??
(stopWatcher = new RelayCommand(obj =>
{
if(watcher == null)
return;
bgColor = "#FF00AB5E";
btnText = "Start";
try
{
watcher.Changed -= funcChangedHandler;
watcher.Dispose();
watcher = null;
}
catch (Exception ex)
{
dialogService.ShowMessage(ex.Message);
}
}));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}