Hi guys,
I have a problem with WebView
. I want to pass some parameters to page that I load from local file such as
player.html?video=https://www.yout.com/watch?v=YXdJ2HLAOdE
I have a solution for iOS
:
string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", "player.html")); var uri = new Uri(fileName); var safeHostUri = new Uri($"{uri.Scheme}://{uri.Authority}", UriKind.Absolute); var safeRelativeUri = new Uri($"{uri.PathAndQuery}{uri.Fragment}?{Element.Uri.Replace("player.html?", "")}", UriKind.Relative); // here I can pass the file and my parameters Control.LoadRequest(new NSUrlRequest(new Uri(safeHostUri, safeRelativeUri)));
function getParameterByName(name) { url = window.location.href; name = name.replace(/[\[\]]/g, '\\$&'); var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, ' ')); }
public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView> { const string JavascriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}"; Context _context; private static Android.Webkit.WebView webView; public HybridWebViewRenderer(Context context) : base(context) { _context = context; } protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e) { base.OnElementChanged(e); try { if (webView == null) { var webViewer = new Android.Webkit.WebView(_context); webViewer.Settings.JavaScriptEnabled = true; webViewer.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}")); SetNativeControl(webViewer); webView = Control; } if (e.NewElement != null) { // here I want to pass some parameters webView.LoadUrl($"file:///android_asset/Content/player.html"); } else { webView.LoadUrl($"file:///android_asset/Content/empty.html"); webView = null; } } catch (Exception ex) { } } }
public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Windows.UI.Xaml.Controls.WebView> { const string JavaScriptFunction = "function invokeCSharpAction(data){window.external.notify(data);}"; private static WebView webView; protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e) { base.OnElementChanged(e); try { if (webView == null) { SetNativeControl(new WebView()); webView = Control; } if (e.NewElement != null) { // here I want to pass some parameters webView.Source = new Uri(string.Format("ms-appx-web:///Content//{0}", Element.Uri)); } else { webView.Source = new Uri(string.Format("ms-appx-web:///Content//{0}", "empty.html")); webView = null; } } catch (Exception ex) { } } async void OnWebViewNavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args) { if (args.IsSuccess) { await Control.InvokeScriptAsync("eval", new[] { JavaScriptFunction }); } } void OnWebViewScriptNotify(object sender, NotifyEventArgs e) { Element.InvokeAction(e.Value); } }
In the player.html
I read the param from JavaScript
. How can I do the same in Android
and UWP
?
Answers
Same thread: https://forums.xamarin.com/discussion/153175/webview-with-local-file-how-pass-parameters#latest