Here’s a very simple repro: Start up VS2010 or VS2008, new a WPF project (.Net Framework 3.5 sp1), add an empty page (Page1.xaml) to the project.
The rest code is in MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TestFrameContent();
}
private void TestFrameContent()
{
FrameworkElement fe = Activator.CreateInstance(Type.GetType("WpfFrameContentProblem.Page1")) as FrameworkElement;
Frame frmContainer = new Frame();
frmContainer.Content = fe;
Debug.Assert(frmContainer.Content != null, "Content is null");
}
}
Run the app, it will fail on Debug.Assert, indicate that frmContainer.Content == null.
It’s really a mystery to me, that a simple assignment will fail. Anyone?
Unfortunately, it is not a simple assignment. Setting the Content property on a Frame actually calls Navigate, which sets the content asynchronously. You will need to handle the Navigated event, which “occurs when the content that is being navigated to has been found, and is available from the Content property, although it may not have completed loading.”