Forum Xamarin Cross Platform with Xamarin

Performance webview xamarin

Goodday,

I am using a xamarin webview in an cross platform application, but the performance compared to the regular browser on my phone is very poor. Loading takes much longer, is there a way the fix this issue?

Posts

  • huangjinshehuangjinshe USMember ✭✭✭

    What platform that the performance very poor?

  • Sander_AertsSander_Aerts NLMember ✭✭

    Android and iOS

  • Sander_AertsSander_Aerts NLMember ✭✭

    But maybe it's the whole app performance, is there something like app priority setting?

  • RyanDixonRyanDixon USMember ✭✭✭

    You could always try enabling hardware/software acceleration on Android and using WKWebView instead of UIWebView in iOS.

    You will need to write some Custom Renderers to achieve this however.

  • Sander_AertsSander_Aerts NLMember ✭✭

    Hi Ryan,

    Thank you for your reply, it's indeed already fixed with a custom renderer.

  • SgXam15SgXam15 INMember ✭✭

    Hi @Sander_Aerts ,

    I was willing to enquire about your implementation.I have floundered upon a situation wherein I am unable to make the web view display any html page or any website for the matter.Its a cross-platform app that me working on.My code is present in Xamarin.forms and I have used a custom renderer to implement dependency service.In IOS on running my code just displays a blank section.Can you please guide me where am I faltering over?

  • Sander_AertsSander_Aerts NLMember ✭✭
    edited December 2016

    What i have done is build the customer renderer with WKWebView, to control the custom renderer i used events. Can you show me your ios and shared code?

  • SgXam15SgXam15 INMember ✭✭

    Hi @Sander_Aerts thank you for reverting back.I just am implementing using PCL and not shared library.The following is the custom renderer of IOS:

    public class ShareWebViewRenderer : ViewRenderer<ShareWebView, WKWebView>,IShareFB, IWKScriptMessageHandler
    {
    const string JavaScriptFunction = "function invokeCSharpAction(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}";
    WKUserContentController userController;

        public string Share()//---------------------------This is the DEPENDENCY SERVICE I am using to connect with PCL
        {
            return NSBundle.MainBundle.BundlePath;
        }
    
        protected override void OnElementChanged(ElementChangedEventArgs<ShareWebView> e)
        {
            base.OnElementChanged(e);
    
            if (Control == null)
            {
                userController = new WKUserContentController();
                var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);
                userController.AddUserScript(script);
                userController.AddScriptMessageHandler(this, "invokeAction");
    
                var config = new WKWebViewConfiguration { UserContentController = userController };
                var webView = new WKWebView(Frame, config);
                SetNativeControl(webView);
            }
            if (e.OldElement != null)
            {
                userController.RemoveAllUserScripts();
                userController.RemoveScriptMessageHandler("invokeAction");
                var hybridWebView = e.OldElement as ShareWebView;
                hybridWebView.Cleanup();
            }
            if (e.NewElement != null)
            {
                string fileName = Path.Combine(NSBundle.MainBundle.BundlePath, string.Format("Content/{0}", Element.Uri));
                Control.LoadRequest(new NSUrlRequest(new NSUrl(fileName, false)));
            }
        }
    
        public void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
        {
            Element.InvokeAction(message.Body.ToString());
        }
    

    the following goes the PCL code for the same:

    var source = new HtmlWebViewSource();
    source.Html = DependencyService.Get().Share();

            var browser = new WebView
            {
                Source = "Test.html",
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand   
            };
        ...
            Content = browser;
    

    Share() interface is inherited and implemented.

    and this PCL code (ContentPage) gets triggered on clicking of a button. Everytime I click the button a blank page gets displayed.The html page "Test.html" is kept under resources folder of Xmarin.IOS.

    Kindly refer.

  • kalyanipwrkalyanipwr Member ✭✭

    @Sander_Aerts said:
    Hi Ryan,

    Thank you for your reply, it's indeed already fixed with a custom renderer.

    hi @Sander_Aerts , i am going thorough same scenario, can you please help. can you please share your custom renderer? which methods you have used in the renderers? it will be really helpful.

Sign In or Register to comment.