forked from xingbiao996/HitokotoComponent
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHitokotoControl.xaml.cs
More file actions
68 lines (64 loc) · 1.91 KB
/
Copy pathHitokotoControl.xaml.cs
File metadata and controls
68 lines (64 loc) · 1.91 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
using System.Net.Http;
using System.Threading.Tasks;
using ClassIsland.Core.Controls;
using System.Windows;
using System.Windows.Threading;
using ClassIsland.Core.Abstractions.Controls;
using ClassIsland.Core.Attributes;
using MaterialDesignThemes.Wpf;
namespace HitokotoComponent
{
[ComponentInfo(
"81941A41-314C-4117-981B-920AEE1E3EB3",
"一言",
PackIconKind.CalendarOutline,
"在主界面显示一言。"
)]
public partial class HitokotoControl : ComponentBase
{
private readonly HttpClient _httpClient = new HttpClient();
public HitokotoControl()
{
InitializeComponent();
LoadHitokotoAsync();
}
public async void LoadHitokotoAsync()
{
try
{
var result = "等待";
if ( PluginSettingsPage.Mode == 0)
{
result = await _httpClient.GetStringAsync("https://v1.hitokoto.cn/?encode=text");
}
else
{
result = await _httpClient.GetStringAsync("https://v1.hitokoto.cn/?encode=text");
}
CheckLength(result,PluginSettingsPage.MaxLength);
}
catch (HttpRequestException)
{
Dispatcher.Invoke(() => HitokotoText.Text = "加载失败");
}
}
/// 预留一个CheckLength方法
/// text是传的文本,
/// maxlength是最大长度
public void CheckLength(string text, int maxlength)
{
if (text.Length > maxlength)
{
LoadHitokotoAsync();
}
else
{
SettingText(text);
}
}
private void SettingText(string text)
{
Dispatcher.Invoke(() => HitokotoText.Text = text);
}
}
}