-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebViewPage.xaml.cs
More file actions
133 lines (120 loc) · 4.46 KB
/
WebViewPage.xaml.cs
File metadata and controls
133 lines (120 loc) · 4.46 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using NGopher.Gopher;
namespace NGopher
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class WebViewPage : Page, IFileSavePickerContinuable
{
private GopherClient _gopher;
private string _content;
private string _selector;
public WebViewPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter == null) return;
var server = e.Parameter as string;
var x = server.Split('\x01');
var y = x[1].Split('\x02');
_gopher = new GopherClient
{
Server = x[0],
Port = Convert.ToUInt16(y[0])
};
_selector = y[1];
NavigateTo(_selector);
}
private async void NavigateTo(string selector)
{
SelectorTextBlock.Text = selector.ToUpper();
var content = await _gopher.MakeTextRequest(selector);
_content = content.ToString();
ContentWebView.NavigateToString(_content);
}
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
var fn = _selector.Split('/').Last().Replace(".txt", "");
var fsp = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.Downloads,
SuggestedFileName = fn
};
fsp.FileTypeChoices.Add("HTML Page", new List<string> { ".html", ".htm" });
fsp.PickSaveFileAndContinue();
}
public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
{
StorageFile file = args.File;
if (file == null) return; // saving cancelled
CachedFileManager.DeferUpdates(file);
await FileIO.WriteTextAsync(file, _content); // save _content to file
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete)
{
// successfully saved file
}
else
{
// oh no
}
}
/// <summary>
/// Handle relative URLs by querying the Gopher server.
/// </summary>
private void ContentWebView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
if (args.Uri.OriginalString.StartsWith("about:"))
{
args.Cancel = true;
var url = args.Uri.OriginalString.Remove(0, 6); // 6 = "about:".length
// let's assume we'll be sent to a HTML page afterwards
NavigateTo(_selector.Substring(0, _selector.LastIndexOf('/')) + "/" + url);
}
/*BackAppBarButton.IsEnabled = ContentWebView.CanGoBack;
ForwardAppBarButton.IsEnabled = ContentWebView.CanGoForward;*/
}
/* TODO: make back/forward work
private void BackAppBarButton_Click(object sender, RoutedEventArgs e)
{
if (ContentWebView.CanGoBack)
ContentWebView.GoBack();
BackAppBarButton.IsEnabled = ContentWebView.CanGoBack;
}
private void ForwardAppBarButton_Click(object sender, RoutedEventArgs e)
{
if (ContentWebView.CanGoForward)
ContentWebView.GoForward();
BackAppBarButton.IsEnabled = ContentWebView.CanGoForward;
}
*/
}
}