-
Notifications
You must be signed in to change notification settings - Fork 88
/
MainWindow.xaml.cs
171 lines (150 loc) · 5.23 KB
/
MainWindow.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
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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.Runtime.InteropServices;
using System.Text;
using Windows.Security.ExchangeActiveSyncProvisioning;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using WinRT;
using WinRT.Interop;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WinUIAppEditor;
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow() => InitializeComponent();
public async void OnOpen()
{
try
{
FileOpenPicker picker = new()
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
};
picker.FileTypeFilter.Add(".txt");
picker.FileTypeFilter.Add(".md");
IntPtr hwnd = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(picker, hwnd);
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
IRandomAccessStreamWithContentType stream = await file.OpenReadAsync();
using DataReader reader = new(stream);
await reader.LoadAsync((uint)stream.Size);
text1.Text = reader.ReadString((uint)stream.Size);
}
}
catch (Exception ex)
{
await ShowErrorAsync(ex.Message);
}
}
public async void OnOpenDotnet()
{
try
{
FileOpenPicker picker = new()
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
};
picker.FileTypeFilter.Add(".txt");
picker.FileTypeFilter.Add(".md");
IntPtr hwnd = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(picker, hwnd);
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
IRandomAccessStreamWithContentType wrtStream = await file.OpenReadAsync();
Stream stream = wrtStream.AsStreamForRead();
StreamReader reader = new(stream);
text1.Text = await reader.ReadToEndAsync();
}
}
catch (Exception ex)
{
await ShowErrorAsync(ex.Message);
}
}
public async void OnSaveDotnet()
{
try
{
FileSavePicker picker = new()
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
SuggestedFileName = "New Document"
};
picker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
IntPtr hwnd = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(picker, hwnd);
StorageFile file = await picker.PickSaveFileAsync();
if (file != null)
{
using StorageStreamTransaction tx = await file.OpenTransactedWriteAsync();
Stream stream = tx.Stream.AsStreamForWrite();
using var writer = new StreamWriter(stream);
byte[] preamble = Encoding.UTF8.GetPreamble();
await stream.WriteAsync(preamble, 0, preamble.Length);
await writer.WriteAsync(text1.Text);
await writer.FlushAsync();
tx.Stream.Size = (ulong)stream.Length;
await tx.CommitAsync();
}
}
catch (Exception ex)
{
await ShowErrorAsync(ex.Message);
}
}
public async void OnSave()
{
try
{
FileSavePicker picker = new()
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
SuggestedFileName = "New Document"
};
picker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
IntPtr hwnd = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(picker, hwnd);
StorageFile file = await picker.PickSaveFileAsync();
if (file != null)
{
using StorageStreamTransaction tx = await file.OpenTransactedWriteAsync();
IRandomAccessStream stream = tx.Stream;
stream.Seek(0);
using DataWriter writer = new(stream);
writer.WriteString(text1.Text);
tx.Stream.Size = await writer.StoreAsync();
await tx.CommitAsync();
}
}
catch (Exception ex)
{
await ShowErrorAsync(ex.Message);
}
}
public void OnClose()
{
App.Current.Exit();
}
private async Task ShowErrorAsync(string message)
{
ContentDialog dlg = new()
{
Title = "Error",
Content = message,
PrimaryButtonText = "OK",
XamlRoot = this.Content.XamlRoot
};
await dlg.ShowAsync();
}
}