Использование обработчика MAUI для управления ActivityIndicator во время навигации WebView на iOS ⇐ IOS
-
Anonymous
Использование обработчика MAUI для управления ActivityIndicator во время навигации WebView на iOS
I have a Xamarin app I'm trying to port to MAUI. It has a WebView control that displays remote HTML content. There is also an ActivityIndicator that is used to indicate when navigation is taking place. On Android, the WebView's Navigating event fires when the page starts loading, and Navigated event fires when the WebView has completed loading the page, as expected. The ActivityIndicator is shown in the Navigation event handler, and hidden in the Navigated event handler. On iOS however, the Navigating event can fire multiple times during a page load, same with the Navigated event. The issue is, a Navigated event does not always fire after the last Navigating event. This often leaves the activity indicator visible after navigation to a new page has completed.
In Xamarin, I worked around this by using a custom renderer for iOS, as shown below. I understand MAUI handlers are supposed to replace Xamarin custom renderers, but none of the examples I've been able to find show how to map to events in the iOS native web view control, as I'm doing. Does anyone know how to do this or if there is a better way to resolve this issue?
Cross-platform layer:
using Xamarin.Forms; namespace MyXamarinApp.CustomControls { public class CustomWebView : WebView { public delegate void NavigateDel(bool flag); public event NavigateDel NavigateEvent; public void CallNavigate(bool flag) { NavigateEvent(flag); } } } iOS layer:
using MyXamarinApp.CustomControls; using MyXamarinApp.iOS.Renderers; using Foundation; using WebKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))] namespace MyXamarinApp.iOS.Renderers { class MyDelegate : WKNavigationDelegate { readonly CustomWebView customView; public MyDelegate(CustomWebView view) { customView = view; } public override void DidStartProvisionalNavigation(WKWebView webView, WKNavigation navigation) { customView.CallNavigate(true); } public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation) { customView.CallNavigate(false); } public override void DidFailNavigation(WKWebView webView, WKNavigation navigation, NSError error) { customView.CallNavigate(false); } } class CustomWebViewRenderer : WkWebViewRenderer { protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e); if (e.NewElement != null) { CustomWebView control = (CustomWebView)Element; this.NavigationDelegate = new MyDelegate(control); } } } }
Источник: https://stackoverflow.com/questions/780 ... vigation-o
I have a Xamarin app I'm trying to port to MAUI. It has a WebView control that displays remote HTML content. There is also an ActivityIndicator that is used to indicate when navigation is taking place. On Android, the WebView's Navigating event fires when the page starts loading, and Navigated event fires when the WebView has completed loading the page, as expected. The ActivityIndicator is shown in the Navigation event handler, and hidden in the Navigated event handler. On iOS however, the Navigating event can fire multiple times during a page load, same with the Navigated event. The issue is, a Navigated event does not always fire after the last Navigating event. This often leaves the activity indicator visible after navigation to a new page has completed.
In Xamarin, I worked around this by using a custom renderer for iOS, as shown below. I understand MAUI handlers are supposed to replace Xamarin custom renderers, but none of the examples I've been able to find show how to map to events in the iOS native web view control, as I'm doing. Does anyone know how to do this or if there is a better way to resolve this issue?
Cross-platform layer:
using Xamarin.Forms; namespace MyXamarinApp.CustomControls { public class CustomWebView : WebView { public delegate void NavigateDel(bool flag); public event NavigateDel NavigateEvent; public void CallNavigate(bool flag) { NavigateEvent(flag); } } } iOS layer:
using MyXamarinApp.CustomControls; using MyXamarinApp.iOS.Renderers; using Foundation; using WebKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))] namespace MyXamarinApp.iOS.Renderers { class MyDelegate : WKNavigationDelegate { readonly CustomWebView customView; public MyDelegate(CustomWebView view) { customView = view; } public override void DidStartProvisionalNavigation(WKWebView webView, WKNavigation navigation) { customView.CallNavigate(true); } public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation) { customView.CallNavigate(false); } public override void DidFailNavigation(WKWebView webView, WKNavigation navigation, NSError error) { customView.CallNavigate(false); } } class CustomWebViewRenderer : WkWebViewRenderer { protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e); if (e.NewElement != null) { CustomWebView control = (CustomWebView)Element; this.NavigationDelegate = new MyDelegate(control); } } } }
Источник: https://stackoverflow.com/questions/780 ... vigation-o
Мобильная версия