Friday, October 30, 2009

Silverlight 3 Page Not Found error

I was working on a project when all of the sudden I started getting a “Page Not Found” error. Of course the page is there and it had been working fine. Turns out it was the templates infrastructure hiding the error from me.

When you create a new SL Navigation Application, you’ll find the following in your MainPage.xml.cs.

// If an error occurs during navigation, show an error window
private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
    e.Handled = true;
    ChildWindow errorWin = new ErrorWindow(e.Uri);
    errorWin.Show();
}

This nice little piece of code hides the real cause of the error so that all you see is Page Not Found.

Bryant Likes has some code that will give you a better idea of the error.

// If an error occurs during navigation, show an error window
private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
    Exception ex = e.Exception;

    while (ex.InnerException != null)
    {
        ex = ex.InnerException;
    }

    e.Handled = true;
    ChildWindow errorWin = new ErrorWindow(ex);
    errorWin.Show();
}

This should save a few hours of looking.

2 comments:

John Kelleher said...

Thanks Paul, found this after repeatedly coming up with 'Page not found' - why is it Silverlight loves to swallow errors - and worse, blatantly mis-label them!

Michael Harper said...

Thanks! the generic error "" was driving me mad!