-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
102 lines (88 loc) · 2.66 KB
/
MainWindow.xaml.cs
File metadata and controls
102 lines (88 loc) · 2.66 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
using System.ComponentModel;
using System.Net.Http;
using System.Windows.Input;
using System.Windows.Threading;
using Newtonsoft.Json.Linq;
using WindowUI.Pages;
using Wpf.Ui.Controls;
namespace WindowUI;
public partial class MainWindow : INotifyPropertyChanged
{
// 创建一个静态的HttpClient实例
private static readonly HttpClient client = new();
private readonly DispatcherTimer _timer;
private string _currentTime = "";
public MainWindow()
{
InitializeComponent();
DataContext = this;
_timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(1)
};
_timer.Tick += Timer_Tick!;
_timer.Start();
LoadApiDataAsync();
}
public string CurrentTime
{
get => _currentTime;
set
{
_currentTime = value;
OnPropertyChanged(nameof(CurrentTime));
}
}
public event PropertyChangedEventHandler? PropertyChanged;
private void Timer_Tick(object sender, EventArgs e)
{
CurrentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void NavigationView_Loaded(object sender, RoutedEventArgs e)
{
// Navigate to the default page
var navigationView = sender as NavigationView;
navigationView?.Navigate(typeof(Home));
}
public async void LoadApiDataAsync()
{
try
{
if (Properties.Settings.Default.ShowYiYan)
{
TitleTextBlock.Text = "加载中。。。";
var apiResponse = await GetApiDataAsync("https://api.nxvav.cn/api/yiyan/");
//解析json
var json = JObject.Parse(apiResponse);
TitleTextBlock.Text = json["yiyan"]?.ToString();
}
else
{
TitleTextBlock.Text = "";
}
}
catch (Exception)
{
TitleTextBlock.Text = "当前无网络连接";
}
}
//异步获取每日一句
private static async Task<string> GetApiDataAsync(string url)
{
// 发送异步GET请求
var response = await client.GetAsync(url);
// 确保请求成功
response.EnsureSuccessStatusCode();
// 读取响应内容
var responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
{
LoadApiDataAsync();
}
}