1

Is there way to use WebView without actual page?

I tried to instantiate webview via new WebView() and then call Navigate to url but navigation completed handler that I register for is not called.

user1744147
  • 1,099
  • 13
  • 30

1 Answers1

1

I tried to instantiate webview via new WebView() and then call Navigate to url but navigation completed handler that I register for is not called.

We need to add the WebView control into to a xaml container(Grid, StackPanel etc), then the WebView.NavigationCompleted event will be called when the WebView has finished loading the current content or if navigation has failed.

For example:

XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="rootGrid">

</Grid>

Code Behind:

public MainPage()
    {
        this.InitializeComponent();
        WebView wb = new WebView();
        wb.Source = new Uri("http://www.microsoft.com");
        wb.NavigationCompleted += Wb_NavigationCompleted;
        rootGrid.Children.Add(wb);
    }

    private void Wb_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
    {
        throw new NotImplementedException();
    }

If you don't want to show WebView page in your UI, just set Visibility property to Collapsed

wb.Visibility = Visibility.Collapsed;

------Update 07/28/2016-------

If you just need to load html page from code behind, the WebView control is not the right choice, there are two solutions:

  1. Using HttpClient class to request webpage

    Check the official sample

  2. Using third-party library, for example, Html Agility Pack, please see Sunteen's answer in this question: HtmlAgility:no contents appeared (C#,UWP)

Community
  • 1
  • 1
Franklin Chen - MSFT
  • 4,845
  • 2
  • 17
  • 28