Monday, September 11, 2017

WebView blank in Xamarin Forms on UWP

TLDR;

If you are hooking up events for a Xamarin Forms WebView, wrap the hooking up in a Device.BeginInvokeOnMainThread if you experience a blank WebView. I've seen this issue mostly on UWP.

    public MyView()
    {
        this.InitializeComponent();
       
        Device.BeginInvokeOnMainThread(() =>
        {
            // Hook up events in here
            this.myWebView.Navigating += async (s, e) =>
            {
                // Do stuff here
            };
        });
    }

Longer version

I have a login view in my app and the code looks like this.

    public partial class LoginView : ContentPage
    {
        public LoginView(LoginViewModel vm)
        {
            this.InitializeComponent();
            this.BindingContext = vm;

            this.LoginWebView.Navigating += async (s, e) =>
            {
                this.BusyLabel.IsVisible = true;
                await vm?.Navigating(e.Url);
            };

            this.LoginWebView.Navigated += async (s, e) =>
            {
            
                this.BusyLabel.IsVisible = false;
            };
  
        }
    }

I needed to hook up the Navigating and Navigated events for two reasons;
  1. To display a busy/loading label over the webview
  2. To pass the resulting URL back to the ViewModel
This works... Most of the time. But sometimes the WebView just displays a white square. I tried a bunch of different urls trying to determine if it was something in the html that was messing something up. 

Then I read this post (https://forums.xamarin.com/discussion/63280/xamarin-forms-webview-does-not-work-once-the-navigating-eventhandler-is-setup). It wasn't really about this issue but the title of the post made me think.

Perhaps, if I delay the event hooking-uping it might magically work again. 

So I wrapped the hookups in a Device.BeginInvokeOnMainThread(...) call and it just seems to work.

An other related issue you might experience is this. But this goes for all controls, not only WebView.
  • https://stackoverflow.com/questions/34330446/xamarin-forms-webview-not-showing-up


22 comments:

  1. Testro T3 The quantity of men who have caught wind of these privileged insights or who hone them can securely be thought to be in the millions around the world.
    http://www.drozhealthblog.com/testro-t3-uk/

    ReplyDelete
  2. Testx Core Nor will they be spent utilizing some odd machine or mechanical embly that is mysteriously expected to build estimate.
    http://www.usadrugguide.com/testx-core-review/

    ReplyDelete
  3. I read your post its great thanks. Waiting for more new update.

    ReplyDelete
  4. CLA Safflower Oil Dropping greater pounds from the frame may be hard, however no longer impossible. Why no longer, provide a attempt to starvation-unfastened and revolutionary way to shed pounds by way .

    ReplyDelete
  5. Dermagen IQ On the off chance that you invest a great deal of energy in an exceptionally dirtied condition this can likewise negatively affect your skin Dermagen IQ for UK.

    ReplyDelete
  6. Dermagen IQ It has common mending properties which help in taking the lost wellbeing and excellence back to the skin.

    ReplyDelete
  7. Rapiture Muscle Builder Berries, nuts and seeds. Piddling as these are, berries, nuts and seeds comprise all of the essential elements that could promote body building Rapiture Muscle for Spain.

    ReplyDelete
  8. Perlelux protein, fabricated in a research facility Its structure is unique in relation to that of the regular one found in your skin.
    http://www.drozhelp.com/perlelux/

    ReplyDelete
  9. D BAL Max Carbohydrates sugar glucose fuel for your muscle tissues While carbs are consumed, they'll be broken down by using.
    http://xtrfact.com/d-bal-max-review/

    ReplyDelete
  10. Slimfire Forskolin We can wrap our brains on the need to get more fit Forskolin The issue is, the vast majority of us stop here.
    http://supplementdigestdog.com/slimfire-forskolin/

    ReplyDelete
  11. Xtrfact you are in the driving seat Not exclusively do you have a full attention to precisely what the fixings.
    http://xtrfact.com/

    ReplyDelete
  12. Pure CBD Oil No other single plant source has the fundamental amino acids in such an effortlessly edible frame, nor has the basic.
    http://www.drozhealthblog.com/pure-cbd-oil/

    ReplyDelete
  13. Dermagen IQ Brutal concoction substances can prompt unfortunate skin as well as trigger other Dermagen IQ for UK medical problems So it is vital to find.
    http://www.drozhealthblog.com/dermagen-iq/

    ReplyDelete
  14. Perlelux Making it impossible to the beams of the sun There is no known reason how individuals create rosacea or how they obtain it.
    http://www.usadrugguide.com/perlelux-ca/

    ReplyDelete
  15. Nitridex It improves the regular magnificence of a lady and lessens the dread of having a monstrous appearance. Things that influence.
    http://supplementdigestdog.com/nitridex/

    ReplyDelete
  16. Zylix Plus Further, not just the spilling gel may gather in the bosom yet another scar may conform to it. At times, gel can relocate .
    http://www.drozhealthblog.com/zylix-plus/

    ReplyDelete
  17. I think this is a useful post and it is exceptionally valuable and learned. along these lines, I might want to thank you for the endeavors you have made in composing this article.


    WorldWide Life Styles
    Download AcMarket

    ReplyDelete
  18. Thanks for sharing this, I've been porting my Xamarin.Forms app from iOS and Android which has never had this issue. Super glad I googled for the issue before wasting a ton of time.

    ReplyDelete
  19. Make sure to check out this article for some info on how to write literary essay outline. This info could be really useful in the future

    ReplyDelete
  20. I am unquestionably making the most of your site. You certainly have some extraordinary understanding and incredible stories. Visit Fifa 2018 live streaming

    ReplyDelete