question

David-7465 avatar image
0 Votes"
David-7465 asked David-7465 commented

How to update a bindableproperty in HybridWebView after page finished loading

Hi

I have been trying this without any success.

I have got a HybridWebView and I would like to update a BindableProperty when a page is finished loading.

the property was correctly set up like this:


         public static readonly BindableProperty IsLoggedInProperty = BindableProperty.Create(
            propertyName: "IsLoggedIn",
            returnType: typeof(bool),
            declaringType: typeof(HybridWebView),
            defaultValue: default(bool));
    
         public bool IsLoggedIn
         {
             get { return (bool)GetValue(IsLoggedInProperty); }
             set { SetValue(IsLoggedInProperty, value); }
         }


I first tried the Navigated event in HybridWebView. but this is not reliable as iOS and Android got their own methods.

I would like to update IsLoggedIn from Android and iOS projects when the pages are loaded. My reason for this, is I am checking a session cookie name. How can I apply this?

I have also tried OnElementChanged in both platforms, but that is not the right event for when the page finished loading.

Thanks

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
1 Vote"
ColeXia-MSFT answered David-7465 commented

Hello,

Welcome to Microsoft Q&A!

Create Custom Renderer for HybridWebView and implement on each platform .

Android

[assembly: ExportRenderer(typeof(HybridWebView), typeof(MyRenderer))]
namespace FormsApp.Droid
{
    public class MyRenderer : WebViewRenderer
    {
        Context _context;

        public MyRenderer(Context context) : base(context)
        {
            _context = context;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                Control.SetWebViewClient(new MyViewClient(this));
            }
        }

    }

    public class MyViewClient : FormsWebViewClient
    {
        MyRenderer _renderer;

        public MyViewClient(MyRenderer renderer) : base(renderer)
        {
            _renderer = renderer;
        }

        public override void OnPageFinished(Android.Webkit.WebView view, string url)
        {
            base.OnPageFinished(view, url);

            HybridWebView webview = _renderer.Element as HybridWebView;
            webview.IsLoggedIn = true;
        }

    }
}


iOS

[assembly: ExportRenderer(typeof(HybridWebView), typeof(MyRenderer))]
namespace FormsApp.iOS
{
    class MyRenderer : WkWebViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if(e.NewElement != null)
            {
                this.NavigationDelegate = new MyDelegate(this);
            }
        }
    }

    class MyDelegate : WKNavigationDelegate
    {
        MyRenderer _renderer;
        public MyDelegate(MyRenderer renderer)
        {
            _renderer = renderer;
        }

        public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            base.DidFinishNavigation(webView, navigation);
            HybridWebView webview = _renderer.Element as HybridWebView;
            webview.IsLoggedIn = true;
        }
    }
}


Best Regards,
Cole Xia


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.