Detect Scrolled to end of webview to enable a button in maui

Keerthana Krishna 60 Reputation points
2024-05-03T03:37:30.4166667+00:00

I have a custom renderer like this where ScrollWebView is my custom control from webview with IsBottomReached property. I tried registering this Renderer in mauiprogram class, then it started crashing with an exception of "Specified cast is not valid". Using this renderer, I am trying to find out if I have reached the end of webview. Please suggest me how I can achieve it.

Thanks in advance

public class ScrollWebViewRenderer : WebViewRenderer

{

public static ScrollWebView view = null;

public ScrollWebViewRenderer(Context context) : base(context)

{

}

protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)

{
base.OnElementChanged(e);

view = (ScrollWebView)Element;

if (Control != null)

{

    Control.ScrollChange += Control_ScrollChange;

}
}

private void Control_ScrollChange(object sender, ScrollChangeEventArgs e)

{
var nativeWebView = e.V as global::Android.Webkit.WebView;
int height = (int)Math.Floor(nativeWebView.ContentHeight * nativeWebView.Scale);
int webViewheight = nativeWebView.Height;
int cutOff = height - webViewheight;
if (e.ScrollY >= cutOff - 40)
{
view.IsBottomReached = true;
LMSGlobal.IsScrolledBottom = true;
}
else
{
view.IsBottomReached = false;
LMSGlobal.IsScrolledBottom = false;
}
}

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,084 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,121 Reputation points Microsoft External Staff
    2024-05-03T06:47:00.4333333+00:00

    Hello,

    Renderer is deprecated in MAUI, where you can use Handler to implement this feature.

    Please refer to the following code, I tested it on Android and it triggers the event as expected when scrolling to the bottom.

    
    <local:ScrollWebView x:Name="scrollWeb" Source="
    
    https://cn.bing.com/"
    
    HeightRequest="300"/>
    
    

    in code-behind:

    
    protected override void OnHandlerChanged()
    
            {
    
                base.OnHandlerChanged();
    
    #if ANDROID
    
                var s =  scrollWeb.Handler.PlatformView as Android.Webkit.WebView;
    
                if (s != null) {
    
                    s.ScrollChange += (s, e) =>
    
                    {
    
                        var nativeWebView = e.V as global::Android.Webkit.WebView;
    
                        
    
                        int height = (int)Math.Floor(nativeWebView.ContentHeight * nativeWebView.Scale);
    
                        int webViewheight = nativeWebView.Height;
    
                        int cutOff = height - webViewheight;
    
                        if (e.ScrollY >= cutOff - 40)
    
                        {
    
                            scrollWeb.IsBottomReached = true;
    
                       }
    
                        else
    
                        {
    
                            scrollWeb.IsBottomReached = false;
    
                        }
    
                    };
    
                }
    
    #endif
    
            }
    
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    2 people 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.