Source,类型 WebViewSource为 ,表示 显示的位置 WebView 。
<WebView Source="https://learn.microsoft.com/dotnet/maui" />等效 C# 代码如下:
WebView webvView = new WebView { Source = "https://learn.microsoft.com/dotnet/maui" };URI 必须使用指定的协议完全形成。
<WebView> <WebView.Source> <HtmlWebViewSource Html="<HTML><BODY><H1>.NET MAUI</H1><P>Welcome to WebView.</P></BODY><HTML>" /> </WebView.Source> </WebView>在 XAML 中,HTML 字符串可能因转义 < 和 > 符号而变得不可读。 因此,为了提高可读性,可以在节 CDATA 中内联 HTML:
<WebView> <WebView.Source> <HtmlWebViewSource> <HtmlWebViewSource.Html> <![CDATA[ <HTML> <BODY> <H1>.NET MAUI</H1> <P>Welcome to WebView.</P> </BODY> </HTML> ]]> </HtmlWebViewSource.Html> </HtmlWebViewSource> </WebView.Source> </WebView>等效 C# 代码如下:
WebView webView = new WebView { Source = new HtmlWebViewSource { // 堆代码 duidaima.com Html = @"<HTML><BODY><H1>.NET MAUI</H1><P>Welcome to WebView.</P></BODY></HTML>" } };
<WebView> <WebView.Source> <HtmlWebViewSource> <HtmlWebViewSource.Html> <![CDATA[ <html> <head> </head> <body> <h1>.NET MAUI</h1> <p>The CSS and image are loaded from local files!</p> <p><a href="localfile.html">next page</a></p> </body> </html> ]]> </HtmlWebViewSource.Html> </HtmlWebViewSource> </WebView.Source> </WebView>本地 HTML 文件可以加载级联样式表 (CSS) 、JavaScript 和图像(如果它们也已使用 MauiAsset 生成操作添加到应用项目中)。
WebView webView = new WebView(); ... webView.Reload();执行导航
WebView webView = new WebView(); ... // Go backwards, if allowed. if (webView.CanGoBack) { webView.GoBack(); } // Go forwards, if allowed. if (webView.CanGoForward) { webView.GoForward(); }在 中 WebView以编程方式启动或由用户启动的页面导航时,会发生以下事件:
Navigated,在页面导航完成时引发。 WebNavigatedEventArgs事件附带Navigated的对象定义Result指示导航结果的 类型的WebNavigationResult属性。
<uses-permission android:name="android.permission.CAMERA" />2.在应用中的某个时间点(例如加载包含 WebView 控件的页面时),请求用户授予权限以允许应用访问相机。
private async Task RequestCameraPermission() { PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.Camera>(); if (status != PermissionStatus.Granted) await Permissions.RequestAsync<Permissions.Camera>(); }3.将以下类添加到 “平台/Android ”文件夹,更改根命名空间以匹配项目的命名空间:
using Android.Webkit; using Microsoft.Maui.Handlers; using Microsoft.Maui.Platform; namespace MauiAppWebViewHandlers.Platforms.Android; internal class MyWebChromeClient: MauiWebChromeClient { public MyWebChromeClient(IWebViewHandler handler) : base(handler) { } public override void OnPermissionRequest(PermissionRequest request) { // 堆代码 duidaima.com foreach (var resource in request.GetResources()) { // Check if the web page is requesting permission to the camera if (resource.Equals(PermissionRequest.ResourceVideoCapture, StringComparison.OrdinalIgnoreCase)) { // Get the status of the .NET MAUI app's access to the camera PermissionStatus status = Permissions.CheckStatusAsync<Permissions.Camera>().Result; // Deny the web page's request if the app's access to the camera is not "Granted" if (status != PermissionStatus.Granted) request.Deny(); else request.Grant(request.GetResources()); return; } } base.OnPermissionRequest(request); } }在前面的代码片段中 MyWebChromeClient , 类继承自 MauiWebChromeClient,并重写 OnPermissionRequest 方法以截获网页权限请求。 检查每个权限项以查看它是否与表示相机的 PermissionRequest.ResourceVideoCapture 字符串常量匹配。 如果相机权限匹配,代码将检查应用是否有权使用该相机。 如果具有权限,则会授予网页的请求。
((IWebViewHandler)theWebViewControl.Handler).PlatformView.SetWebChromeClient(new MyWebChromeClient((IWebViewHandler)theWebViewControl.Handler));.还可以使用处理程序属性映射来强制所有 WebView 控件使用 Chrome 客户端。
private static void CustomizeWebViewHandler() { #if ANDROID26_0_OR_GREATER Microsoft.Maui.Handlers.WebViewHandler.Mapper.ModifyMapping( nameof(Android.Webkit.WebView.WebChromeClient), (handler, view, args) => handler.PlatformView.SetWebChromeClient(new MyWebChromeClient(handler))); #endif }
using System.Net; CookieContainer cookieContainer = new CookieContainer(); Uri uri = new Uri("https://learn.microsoft.com/dotnet/maui", UriKind.RelativeOrAbsolute); Cookie cookie = new Cookie { Name = "DotNetMAUICookie", Expires = DateTime.Now.AddDays(1), Value = "My cookie", Domain = uri.Host, Path = "/" }; cookieContainer.Add(uri, cookie); webView.Cookies = cookieContainer; webView.Source = new UrlWebViewSource { Url = uri.ToString() };在此示例中,将一个 Cookie 添加到 CookieContainer 对象中,然后将该对象设置为 属性的值 WebView.Cookies 。 WebView当 将 Web 请求发送到指定的 URL 时,Cookie 随请求一起发送。
Entry numberEntry = new Entry { Text = "5" }; Label resultLabel = new Label(); WebView webView = new WebView(); ... int number = int.Parse(numberEntry.Text); string result = await webView.EvaluateJavaScriptAsync($"factorial({number})"); resultLabel.Text = $"Factorial of {number} is {result}.";方法 WebView.EvaluateJavaScriptAsync 计算指定为 参数的 JavaScript,并将任何结果返回为 string。 在此示例中, factorial 调用 JavaScript 函数,该函数将返回 作为结果的 number 阶乘。 此 JavaScript 函数在 加载的本地 HTML 文件中 WebView 定义,并在以下示例中显示:
<html> <body> <script type="text/javascript"> function factorial(num) { if (num === 0 || num === 1) return 1; for (var i = num - 1; i >= 1; i--) { num *= i; } return num; } </script> </body> </html>
可以使用 提供的 类Microsoft.Maui.Essentials在系统 Web 浏览器中Launcher打开 URI。 调用启动器 OpenAsync 的方法,并传入 string 表示要打开的 URI 的 或 Uri 参数:
await Launcher.OpenAsync("https://learn.microsoft.com/dotnet/maui");