【问题标题】:Preloading a Page before Navigating to it WPF在导航到页面之前预加载页面 WPF
【发布时间】:2010-10-22 21:00:18
【问题描述】:

我的 WPF 应用程序包含一个 NavigationWindow,然后是一组定义为单独 xaml 文件的 Pages。 NavigationWindow 依次加载并显示各个页面。

我的问题是加载页面很昂贵并且可能会失败。因此,我想在后台预加载页面,然后只在页面加载完成后调用Navigate()

在伪代码中我想要的是

    Page nextPage;
    try
    {
    nextPage = LoadPageFromURI(new URI(...));
    }
    catch
    {
/// constructor of the page threw an exception ... load a different page
    }

    myNavigationWindow.Navigate(nextPage);

但是我找不到框架函数来做我想做的事。了解WPF的人可以更好地帮助我吗?谢谢你!

【问题讨论】:

  • 你使用什么模式?什么是昂贵的操作?数据查询还是别的什么?
  • 这是一个小应用程序,所以它没有使用任何像 MVVM 这样的模式——它只是一个加载许多页面的 NaviationWindow。页面本身确实在其构造函数中进行数据查询(通过 ADO.NET)......这些操作很长,有时可能会失败。
  • 如果您认为昂贵的操作应该转移到其他地方,请接受您的建议。谢谢!

标签: wpf navigation


【解决方案1】:

看起来Application.LoadComponent() 会做我想做的事。

示例代码:

Page page;

try
{
    page = (Page) Application.LoadComponent(new Uri(path, UriKind.Relative));
}
catch (Exception ex)
{
    // note error and abort
}

Action action = () => ((NavigationWindow)Application.Current.MainWindow).Navigate(page);
Application.Current.Dispatcher.BeginInvoke(action, DispatcherPriority.Normal);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多