How to update a bindableproperty in HybridWebView after page finished loading

David 356 Reputation points
2021-05-14T15:36:40.377+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-05-17T02:01:09.567+00:00

    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 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.