How to show progressbar until WP8 WebBrowser contr

2019-09-06 00:27发布

问题:

I have a WebBrowser control and I want to show some url on this control. Until the webbrower loaded the page, I need to show some progressbar or animation.

Please help me, here's what I have:

XAML:

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <ProgressBar x:Name="progressbar" IsIndeterminate="True"/>
    <phone:WebBrowser x:Name="webbrw" IsScriptEnabled="True"/>
</Grid>

strong text

public MainPage()
{
    InitializeComponent();

    // Sample code to localize the ApplicationBar
    //BuildLocalizedApplicationBar();
    Loaded += MainPage_Loaded;
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    webbrw.LoadCompleted += webbrw_LoadCompleted;
    webbrw.Source = new Uri("http://www.youtube.com/user/tseries");
}

void webbrw_LoadCompleted(object sender, NavigationEventArgs e)
{
    progressbar.IsIndeterminate = false;
}

回答1:

If you simply want to show the bar while loading the page and hide it afterwards, you're using the wrong property. The IsIndeterminate property reports a generic process (true) or one based on a value (false). To hide the progressbar you should use:

progressbar.Visibility = Visibility.Collapsed; 


回答2:

This can be achieved by using LoadCompleted property. Hope this solves your problem.

XAML Should be like:

<ProgressBar x:Name="progressbar" IsIndeterminate="True"/>
<phone:WebBrowser x:Name="webbrw" IsScriptEnabled="True" LoadCompleted="yesLoaded"/>

C# for this;

    private void yesLoaded(object sender, NavigationEventArgs e)
    {
        this.progressbar.Visibility = Visibility.Collapsed; 
        this.progressbar.IsIndeterminate = False;
    }


回答3:

Try using the Navigate method and catch Navigated event instead which triggers after the control navigates successfully. Check the MSDN reference http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.controls.webbrowser_events(v=vs.105).aspx



回答4:

To handle every possible case, i had to register to these two WebBrowser events LoadCompleted and NavigationFailed.

And of course, suspending the progress in each callback/handler with :

 progressbar.IsIndeterminate = false;


回答5:

I used OnNavigatedTo method to initialize the progressbar as shown below:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            progressbar.IsIndeterminate = true;
            //u can add other codes you need
        }

then check if the browser is fully loaded using LoadCompleted method as shown below:

 private void Browser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            progressbar.IsIndeterminate = false;
        }

notice that the progressbar is a Name I gave to the ProgressBar on xaml and