Tuesday, September 18, 2012

Handling xaml not found error while navigating to non-existing xaml form

In some cases in your silverlight application the link is present for non-existing or old xaml view form. which does not exist in actual or removed after any updation. In this case while nevigating to this form silverlight runtime exception will occur. To handle this exception you need to add NavigationFailed event for the frame.

In Xaml:

<sdk:Frame  Height="175" HorizontalAlignment="Left" Margin="48,92,0,0" Name="frame1" VerticalAlignment="Top" Width="293" BorderBrush="#006C1313" NavigationFailed="frame1_NavigationFailed" />

In Xaml.cs


private void frame1_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
            e.Handled = true;
            this.frame1.Navigate(new Uri("/Testview/ErrorPage.xaml", UriKind.Relative));
}

How to navigate the frame to another view in another silver-light project under same solution

You can navigate from one view to another view in different silver-light project under same solution by using the following code.



private void button1_Click(object sender, RoutedEventArgs 
{



this.frame1.Navigate(new Uri("/MyNewSDXProject;component/TestViews/MyTestUC.xaml", UriKind.Relative));
}


Xaml will look like this:


<UserControl x:Class="SilverlighProjectTestingApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
  
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="48,47,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <sdk:Frame  Height="175" HorizontalAlignment="Left" Margin="48,92,0,0" Name="frame1" VerticalAlignment="Top" Width="293" BorderBrush="#006C1313" NavigationFailed="frame1_NavigationFailed" />
    </Grid>
</UserControl>