My WPF app consists of a NavigationWindow
, and then a set of Page
s defined as separate xaml files. The NavigationWindow loads and displays the various pages in turn.
My problem is that loading the p开发者_JS百科ages is expensive and may fail. Thus, I want to preload the page in the background, and then only call Navigate()
once the page has finished loading.
In pseudocode what I would want is
Page nextPage;
try
{
nextPage = LoadPageFromURI(new URI(...));
}
catch
{
/// constructor of the page threw an exception ... load a different page
}
myNavigationWindow.Navigate(nextPage);
I can't however find framework functions to do what I want. Could someone who knows WPF better give me a hand? Thanks you!
It looks like Application.LoadComponent()
will do what I want.
Sample code:
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);
精彩评论