NavigationProgress 不显示进度条

发布于 2024-11-29 13:05:10 字数 8 浏览 1 评论 0原文

continue

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分开我的手 2024-12-06 13:05:10

进度事件对我来说似乎毫无用处,而文档声称导航是同步发生的(如果您使用 Navigate 方法),我无法确定它的哪一部分应该是异步的。

如果您有一个大型 XAML 会冻结 UI,如果您有一个长时间运行的构造函数也会冻结 UI,并且如果您导航到网页,则进度事件甚至不会被触发。

至少其中一些是有意义的,因为控件是线程相关的,如果在不同线程上异步创建对象会导致问题。如果您有一个长时间运行的构造函数,则需要将工作外部化到本地线程,因为构造函数本身需要在 UI 线程上调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文