es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

El evento Navigated de Xamarin.macOS no se activa para CustomWebView.

He creado un renderizador personalizado para mi WebView porque necesito establecer la propiedad CustomUserAgent. Pero para el evento Navigated de mi CustomWebView no se dispara. Según entiendo, necesito establecer NavigationDelegate pero no puedo encontrar ningún ejemplo que funcione. Aquí está mi código:

“`
[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace DesktopClient.MacOS.CustomRenderers
{
public class CustomWebViewRenderer : ViewRenderer<customwebview, wkwebview="">
{
private WKWebView wkWebView;

<pre><code> protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
{
base.OnElementChanged(e);

if (this.Control == null)
{
WKWebViewConfiguration config = new();

this.wkWebView = new WKWebView(this.Frame, config)
{
NavigationDelegate = new WKNavigationDelegate()
};

this.SetNativeControl(this.wkWebView);

WKWebViewContainer.WKWebView = this.wkWebView;
}

if (e.NewElement != null)
{
UrlWebViewSource source = (UrlWebViewSource)e.NewElement.Source;

if (source != null)
{
this.Control.LoadRequest(new NSUrlRequest(new NSUrl(source.Url)));
}

this.Control.CustomUserAgent = e.NewElement.UserAgent;
this.wkWebView.NavigationDelegate = new WKNavigationDelegate();
}
}
}
</code></pre>

}
“`</customwebview,>

Tags:  , ,

Answer

  1. Avatar for davy.ai

    Necesitas implementar los métodos de WKNavigationDelegate para que se active el evento de Navigated. Aquí tienes un ejemplo de cómo hacerlo:


    public class WKNavigationDelegate : WKNavigationDelegate
    {
    public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
    {
    // Dispara el evento Navigated aquí
    }
    }

Comments are closed.