代码之家  ›  专栏  ›  技术社区  ›  Slaggg

在导航到某个页面之前预加载该页面wpf

  •  2
  • Slaggg  · 技术社区  · 14 年前

    我的WPF应用程序包括 NavigationWindow ,然后是一组 Page 定义为单独的XAML文件。导航窗口依次加载和显示各种页面。

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

    在伪代码中,我想要的是

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

    但是我找不到框架函数来做我想做的事情。有谁知道WPF能帮我一把吗?谢谢你!

    1 回复  |  直到 14 年前
        1
  •  2
  •   Slaggg    14 年前

    看起来像 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);