This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeLine.xaml.cs
168 lines (147 loc) · 6.55 KB
/
TimeLine.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using App4.sources;
using Newtonsoft.Json;
// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板
namespace App4
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class TimeLine : Page
{
private const string Uri = "https://nmb.fastmirror.org/Api/timeline";
private static int _timeLinePage = 1;
private ObservableCollection<PostContent> _postContents;
/* The current structure:
* ContentStackPanel -> grid -> rootStackPanel -> infoStackPanel -> titleTextBlock
* | | -> nameTextBlock
* | | -> noTextBlock
* | |
* | -> contentStackPanel -> image
* | -> textBlock
* -> ...(same grid)
* -> ...
*/
public TimeLine()
{
InitializeComponent();
}
private string GetPostContent(string uri)
{
var httpClientHandler = new HttpClientHandler();
var httpClient = new HttpClient(httpClientHandler);
httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
var result = httpClient.GetStringAsync(uri).Result;
httpClient.Dispose();
return result;
}
private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
{
Grid grid;
grid = sender as Grid;
if (grid != null)
{
var id =
(((grid.Children.ToList()[0] as StackPanel).Children.ToList()[0] as StackPanel).Children
.ToList()[2] as TextBlock).Text;
id = id.Replace("No.", "?id=");
_timeLinePage = 1;
Frame.Navigate(typeof(Thread), id);
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
_postContents = null;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var result = GetPostContent(Uri);
_postContents =
new ObservableCollection<PostContent>(JsonConvert.DeserializeObject<List<PostContent>>(result));
ContentListView.ItemsSource = _postContents;
}
private void rootScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if ((sender as ScrollViewer).VerticalOffset == (sender as ScrollViewer).ScrollableHeight)
{
var newUri = $"{Uri}?page={++_timeLinePage}";
var result = GetPostContent(newUri);
foreach (var thread in new ObservableCollection<PostContent>(
JsonConvert.DeserializeObject<List<PostContent>>(result))) _postContents.Add(thread);
}
}
private void ShowMenu(bool isTransient, object sender)
{
var myOption = new FlyoutShowOptions();
myOption.ShowMode = isTransient ? FlyoutShowMode.Transient : FlyoutShowMode.Standard;
if (sender != null) CommandBarFlyout1.ShowAt(sender as Image, myOption);
}
private async void AppBarButton_Click(object sender, RoutedEventArgs e)
{
var newView = CoreApplication.CreateNewView();
var newViewId = 0;
var test = ((CommandBarFlyout1.Target as Image).GetValue(Image.SourceProperty) as BitmapImage).UriSource;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
var frame = new Frame();
frame.Navigate(typeof(ImageDetail), test);
Window.Current.Content = frame;
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
var viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
}
private void AppBarButton_Click_1(object sender, RoutedEventArgs e)
{
var imageSave = new ImageSave();
ImageSave.SaveImage(((CommandBarFlyout1.Target as Image).GetValue(Image.SourceProperty) as BitmapImage)
.UriSource);
}
private void image_ContextRequested(UIElement sender, ContextRequestedEventArgs args)
{
ShowMenu(false, null);
}
private void image_Tapped(object sender, TappedRoutedEventArgs e)
{
ShowMenu(true, sender as Image);
e.Handled = true;
}
private async void ThreadWebview_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
var webView = sender;
int width;
int height;
// get the total width and height
var widthString = await webView.InvokeScriptAsync("eval", new[] {"document.body.scrollWidth.toString()"});
var heightString = await webView.InvokeScriptAsync("eval", new[] {"document.body.scrollHeight.toString()"});
if (!int.TryParse(widthString, out width)) throw new Exception("Unable to get page width");
if (!int.TryParse(heightString, out height)) throw new Exception("Unable to get page height");
// resize the webview to the content
webView.Width = width;
webView.Height = height;
}
//private void contentStackPanel_Loading(FrameworkElement sender, object args)
//{
// StackPanel stackPanel = sender as StackPanel;
// WebView webView = new WebView();
// webView.MinWidth = 600;
// webView.DefaultBackgroundColor = new Windows.UI.Color { R = 255, G = 255, B = 255, A = 60 };
// webView.SetValue(WebViewEx.UriProperty, "<html><body>res</body></html>");
// stackPanel.Children.Add(webView);
//}
}
}