-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathConfigPage.xaml.cs
81 lines (69 loc) · 2.44 KB
/
ConfigPage.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
using CommunityToolkit.Mvvm.Messaging;
namespace Aila;
public partial class ConfigPage
{
public ConfigPage()
{
InitializeComponent();
InitializeEditorContent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
await InitializeEditorContent();
}
private async Task InitializeEditorContent()
{
try
{
string content = await ReadFromFile("config.txt");
editor.Text = content;
}
catch (Exception ex)
{
// 使用 DisplayAlert 显示错误消息
await DisplayAlert("Error", $"Failed to load content: {ex.Message}", "OK");
}
}
private async void OnSaveButtonClicked(object sender, EventArgs e)
{
try
{
string content = editor.Text.Replace('“', '"') // 中文左双引号
.Replace('”', '"') // 中文右双引号
.Replace('„', '"') // 德文低-9引号
.Replace('«', '"') // 法文左引号
.Replace('»', '"') // 法文右引号
.Replace('“', '"') // 德文左引号
.Replace('”', '"'); // 德文右引号;
await WriteToFile("config.txt", content);
// 可以在这里添加一个成功消息
await DisplayAlert("Success", "Content saved successfully.", "OK");
}
catch (Exception ex)
{
// 使用 DisplayAlert 显示错误消息
await DisplayAlert("Error", $"Failed to save content: {ex.Message}", "OK");
}
// MessagingCenter.Send<ConfigPage>(this, "ConfigUpdated");
WeakReferenceMessenger.Default.Send(new ConfigurationUpdatedMessage());
}
private async void OnCheckConfigUpdateClicked(object sender, EventArgs e)
{
await Launcher.OpenAsync("https://github.com/win4r/AISuperDomain/blob/main/config.json");
}
private async Task<string> ReadFromFile(string fileName)
{
var filePath = Path.Combine(FileSystem.AppDataDirectory, fileName);
if (File.Exists(filePath))
{
return await File.ReadAllTextAsync(filePath);
}
return string.Empty;
}
private async Task WriteToFile(string fileName, string content)
{
var filePath = Path.Combine(FileSystem.AppDataDirectory, fileName);
await File.WriteAllTextAsync(filePath, content);
}
}