1 forms 代码
2
3 using System;
4 using Xamarin.Forms;
5
6 namespace.Core
7 {
8 public class ContentWebView:WebView
9 {
10 //private ProgressBar progressbar;
11
12 public ContentWebView ()
13 {
14
15 }
16
17 public string Url{ get; set;}
18
19 public string LocalFile{ get; set;}
20
21 public string HtmlStr{ get; set;}
22
23 public string Token{ get; set;}
24
25
26
27
28 bool android_EnableLongPress=true;
29 public bool EnableLongPress {
30 get {
31 return android_EnableLongPress;
32 }
33 set {
34 android_EnableLongPress = value;
35 }
36 }
37
38
39 bool useLoading=false;
40 public bool UseLoading {
41 get {
42 return useLoading;
43 }
44 set {
45 useLoading = value;
46 }
47 }
48
49 bool isFinished=false;
50 public bool IsFinished {
51 get {
52 return isFinished;
53 }
54 set {
55 isFinished = value;
56 }
57 }
58
59 public delegate void LoadFinishedHandler( object sender, EventArgs e );
60 public event LoadFinishedHandler LoadFinished;
61
62 public void RiseloadedEvent()
63 {
64 IsFinished = true;
65 if (LoadFinished!=null) {
66 LoadFinished (this, null);
67 }
68
69 }
70 bool fakeWeChat=false;
71 public bool FakeWeChat {
72 get {
73 return fakeWeChat;
74 }
75 set {
76 fakeWeChat = value;
77 }
78 }
79
80 bool scalesPageToFit;
81 public bool ScalesPageToFit {
82 get {
83 return scalesPageToFit;
84 }
85 set {
86 scalesPageToFit = value;
87 }
88 }
89
90
91 }
92 }
93
94 284300.png
95
96 using System;
97 using Xamarin.Forms;
98 using.Core;
99 using Xamarin.Forms.Platform.Android;
100 using System.Collections.Generic;
101 using Android.Webkit;
102 using Android.Content;
103
104
105 [assembly: ExportRenderer(typeof(ContentWebView), typeof(ContentWebViewRender))]
106 namespace.Core
107 {
108 public class ContentWebViewRender:WebViewRenderer
109 {
110 public ContentWebViewRender ()
111 {
112
113 }
114
115 public class CustomWebViewClient : WebViewClient
116 {
117 ContentWebView t;
118
119 public CustomWebViewClient (ContentWebView self)
120 {
121 t = self;
122 }
123
124 public override void OnPageFinished (Android.Webkit.WebView view, string url)
125 {
126 if (t!=null) {
127 t.RiseloadedEvent ();
128 }
129 base.OnPageFinished (view, url);
130
131 }
132 }
133
134 public override bool OnKeyLongPress(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
135 {
136 //base.SetOnLongClickListener
137 return true;
138 //base.OnKeyLongPress(keyCode,e);
139 }
140
141 protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.WebView> e)
142 {
143 base.OnElementChanged (e);
144
145 if (e.OldElement == null) { // perform initial setup
146 var t=e.NewElement as ContentWebView ;
147 //this.Control.SetOnLongClickListener (new LongClickEventArgs);
148 /*
149 if (!t.Android_EnableLongPress) {
150
151 // this.Control.LongClickable = false;
152 // this.LongClick += (o1, e1) => {
153 //
154 // };
155
156 this.SetOnLongClickListener (new longpress ());
157 }
158 */
159 if (t.FakeWeChat) {
160 this.Control.Settings.UserAgentString = "Mozilla/5.0 AppleWebKit/600.1.4 (KHTML, like Gecko) MicroMessenger/5.4.1 Fake";
161 //this.Control.Settings.DefaultTextEncodingName = "gb2312";
162
163 }
164 this.Control.Settings.DefaultTextEncodingName = "gb2312";
165 this.Control.SetWebViewClient (new CustomWebViewClient (t));
166 this.Control.SetWebChromeClient (new CustomChromeClient(t));
167 this.Control.Settings.JavaScriptEnabled = true;
168
169 if (string.IsNullOrEmpty (t.Token)) {
170 if (!string.IsNullOrEmpty (t.Url)) {
171 this.Control.LoadUrl (t.Url);
172 } else if (!string.IsNullOrEmpty (t.HtmlStr)) {
173 this.Control.LoadData (t.HtmlStr, "text/html", "UTF-8");
174
175 } else if (!string.IsNullOrEmpty (t.LocalFile)) {
176 this.Control.LoadUrl ("file:///android_asset/" + t.LocalFile);
177 } else {
178 }
179
180 } else {
181 Dictionary<string, string> headers = new Dictionary<string, string> ();
182 headers.Add ("Authorization", t.Token);
183 try {
184 this.Control.LoadUrl (t.Url, headers);
185 } catch (Exception ex) {
186 Console.WriteLine (ex.ToString ());
187 }
188
189 }
190 Control.LongClick += Control_LongClick;
191 }
192 }
193
194 void Control_LongClick (object sender, LongClickEventArgs e)
195 {
196 //do nothing
197 }
198
199
200 protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
201 {
202 if (Control != null)
203 {
204 Control.Settings.BuiltInZoomControls = true;
205 Control.Settings.DisplayZoomControls = true;
206 }
207 base.OnElementPropertyChanged(sender, e);
208
209 }
210 }
211 }
212
213
214 using System;
215 using System.Diagnostics;
216 using System.Text;
217 using System.Text.RegularExpressions;
218
219 using Xamarin.Forms;
220 using.Core;
221 using Xamarin.Forms.Platform.Android;
222 using System.Collections.Generic;
223
224 using Android.Webkit;
225 using Android.App;
226 using Android.Content;
227
228 namespace.Core
229 {
230 public class CustomChromeClient : WebChromeClient
231 {
232 ContentWebView t;
233 public CustomChromeClient (ContentWebView self)
234 {
235 this.t = self;
236 }
237
238 public override bool OnJsAlert (Android.Webkit.WebView view, string url, string message, JsResult result)
239 {
240 AlertDialog.Builder builder = new AlertDialog.Builder (view.Context);
241 builder.SetTitle ("提示");
242 builder.SetMessage (message);
243 //builder.SetPositiveButton ("确定",new CustomDialogInterfaceOnClickOKListener(result));
244 builder.SetPositiveButton("确定",delegate {
245 result.Confirm();
246 });
247 //builder.SetOnKeyListener (new CustomDialogInterfaceOnKeyListener());
248 builder.SetCancelable (false);
249 AlertDialog dialog = builder.Create ();
250 dialog.Show ();
251 result.Confirm ();
252 return true;
253 //return base.OnJsAlert(view, url, message, result);
254 }
255
256 public override bool OnJsConfirm (Android.Webkit.WebView view, string url, string message, JsResult result)
257 {
258 AlertDialog.Builder builder = new AlertDialog.Builder (view.Context);
259 builder.SetTitle ("提示")
260 .SetMessage (message)
261 .SetPositiveButton ("确定", delegate {
262 result.Confirm();
263 })
264 .SetNeutralButton("取消",delegate {
265 result.Cancel();
266 });
267
268 builder.SetOnCancelListener(new CustomDialogInterfaceOnCancelListener(result));
269 builder.SetOnKeyListener(new CustomDialogInterfaceOnKeyListener());
270 AlertDialog dialog = builder.Create();
271 dialog.Show();
272 return true;
273 //return base.OnJsConfirm (view, url, message, result);
274 }
275 }
276 }
277
278 using System;
279 using System.Diagnostics;
280
281 using Xamarin.Forms;
282 using.Core;
283 using Xamarin.Forms.Platform.Android;
284 using System.Collections.Generic;
285 using Android.Webkit;
286
287 namespace.Core
288 {
289 public class CustomClient : WebViewClient
290 {
291 ContentWebView t;
292
293 public CustomClient (ContentWebView self)
294 {
295 t = self;
296 }
297
298 public override void OnPageFinished (Android.Webkit.WebView view, string url)
299 {
300 if (t!=null) {
301 t.RiseloadedEvent ();
302 }
303 base.OnPageFinished (view, url);
304
305 }
306 }
307 }
308
309 using System;
310 using Xamarin.Forms;
311
312 using Android.Content;
313 using Android.Widget;
314 using Android.App;
315 using Android.Views;
316
317 namespace.Core
318 {
319 public class CustomDialogInterfaceOnCancelListener: Java.Lang.Object,IDialogInterfaceOnCancelListener
320 {
321 Android.Webkit.JsResult result;
322
323 public CustomDialogInterfaceOnCancelListener (Android.Webkit.JsResult self)
324 {
325 result = self;
326 }
327
328 public void OnCancel (IDialogInterface dialog)
329 {
330 result.Cancel ();
331 }
332 }
333 }
334
335 using System;
336 using Xamarin.Forms;
337
338 using Android.Content;
339 using Android.Widget;
340 using Android.App;
341 using Android.Views;
342
343 namespace.Core
344 {
345 public class CustomDialogInterfaceOnKeyListener:Java.Lang.Object,IDialogInterfaceOnKeyListener
346 {
347 public CustomDialogInterfaceOnKeyListener ()
348 {
349 }
350
351 public bool OnKey (IDialogInterface dialog, Keycode keyCode, KeyEvent e){
352 return true;
353 }
354 }
355 }