开发者

NavigationProgress doesn't show progressbar

I'm writing a WPF application using a navigation frame. I'm trying to display a progress-bar while waiting for a page to be loaded.

Like the msdn page sais:

Navigating : Occurs when a new navigation is requested. Can be used to cancel the navigation.

NavigationProgress : Occurs periodically during a download to provide navigation progress information.

Navigated : Occurs when the page has been located and downloaded.

So I have a Grid (navigationStatusGrid) with ProgressBar (navigationProgressBar) and I have a Frame (mainFrame)

These are my event handlers:

private void mainFrame_NavigationProgress(object sender, NavigationProgressEventArgs e)
{
     long progress = e.BytesRead * 100 / e.MaxBytes;
     Console.WriteLine("Navigating progress:" + progress + "%");
     navigationProgressBar.Value = progress;
}

private void mainFrame_Navigating(object sender, NavigatingCancelEventArgs e)开发者_高级运维
{
     Console.WriteLine("start navigating");
     mainFrame.Visibility = Visibility.Hidden;
     navigationStatusGrid.Visibility= Visibility.Visible;
}

private void mainFrame_Navigated(object sender, NavigationEventArgs e)
{
     Console.WriteLine("end navigating");
     mainFrame.Visibility = Visibility.Visible;
     navigationStatusGrid.Visibility = Visibility.Hidden;
}

This is my output:

start navigating
Navigating progress:4%
Navigating progress:8%
...
Navigating progress:87%
Navigating progress:92%
Navigating progress:96%
Navigating progress:99%
Navigating progress:100%
Navigating progress:100%
end navigating

So you would say it works, but somehow the UI only gets updated when the page is loaded. The UI even freezes up while loading... I don't get to see the progressbar just instantly the loaded page. How do I solve this?


The progress event seems rather useless to me, while the documentation claims that the navigation happens assunchronously (if you use the Navigate method) i cannot make out just which part of it is supposed to be assynchronous.

If you have a large XAML that will freeze up the UI, if you have a long running contructor that will freeze up the UI as well and if you navigate to a web-page the progress event is not even fired.

At least some of this makes sense as controls are thread-afine, if the object were to be created asynchronously on a different thread that would cause problems. If you have a long running consctructor you need to externalize the work to a thread locally as the constructor itself needs to be called on the UI-thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜