I’m using the Silverlight business application (Navigation application) template that comes with Visual studio 2010. I have 3 pages in my app ..Home, About and Contact.
Now my requirement is that, based on a certain configuration setting stored in the web.config file, retrieved via an async WCF call, I need to do 2 things via code behind
1. Add HyperLinks to the one or more of the above pages
2. And also make the content frame in the MainPage.Xaml to load a particular page by default
My MainPage.Xaml looks like following
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
x:Class="MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel x:Name="LinksStackPanel"
Style="{StaticResource LinksStackPanelStyle}">
</StackPanel>
<navigation:Frame x:Name="ContentFrame"
Navigated="ContentFrame_Navigated"
NavigationFailed="ContentFrame_NavigationFailed">
<navigation:Frame.UriMapper>
<uriMapper:UriMapper>
<uriMapper:UriMapping Uri=""
MappedUri="/Views/Home.xaml" />
<uriMapper:UriMapping Uri="/{pageName}"
MappedUri="/Views/{pageName}.xaml" />
<uriMapper:UriMapping MappedUri="/Views/{pageName}.xaml"
Uri="{}{pageName}" />
</uriMapper:UriMapper>
</navigation:Frame.UriMapper>
</navigation:Frame>
In my code behind of MainPage.xaml.cs I do the following
/// <summary>
/// Default constructor
/// </summary>
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// Determine if deployed in TEST or PROD
Client.IsTestCompleted += new EventHandler<IsTestCompletedEventArgs>(Client_IsTestCompleted);
client.IsTestAsync();
}
void Client_IsTestCompleted(object sender, IsTestCompletedEventArgs e)
{
MainPage.IsTest = e.Result;
if (MainPage.IsTest)
{
// Add hyperling to About page
this.AddHyperLink("About", "about");
// load about.xaml in the content frame
this.ContentFrame.Source = new Uri("About", UriKind.Relative);
}
else
{
// Add hyperlinks to Home, contact and About page
this.AddHyperLink("Home", "home");
this.AddHyperLink("Contact", "contact");
this.AddHyperLink("About", "about");
// load about.xaml in the content frame
this.ContentFrame.Source = new Uri("About", UriKind.Relative);
}
}
private void AddHyperLink(string uri, string content)
{
HyperlinkButton hb = new HyperlinkButton();
hb.NavigateUri = new Uri(uri, UriKind.RelativeOrAbsolute);
hb.TargetName = "ContentFrame";
hb.Content = content;
hb.Style = Application.Current.Resources["LinkStyle"] as Style;
this.LinksStackPanel.Children.Add(hb);
}
So this working as expected. That is when I start the application it loads the about page only when deployed in test and when deployed in prod shows 3 links with content frame navigated to about page.
But the problem is, the constructor of the HomePage, Home_Loaded and OnNavigatedTo() are getting called from somewhere before the similar methods in AboutPage are called.
This is wasting a lot of time bcoz, it unnecessarily execute the code in these methods when its not being loaded in the frame
This is code in Home.Xaml.cs
public Home()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Home_Loaded);
}
/// <summary>
/// Load the home page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Home_Loaded(object sender, RoutedEventArgs e)
{
//Expensive code
}
/// <summary>
/// Executes when the user navigates to this page.
/// </summary>
/// <param name="e"></param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//Expensive code
}
So my questions are…
-
Who is calling these methods in HomePage and why? Since I was loading about.xaml in the content frame I dont expect any code in HomePage to execute but only the code in About.xaml
-
And what do i need to do to make sure that these methods in the HomePage are not executed on application load but only when navigated to that page by clicking on “Home” link.
-
Is there a better way to achieve this navigation problem , other than setting the source of the content frame
Thanks for the help
I did what Rikkos suggested. Changed the Uri mapping of default page to an empty page and it works like a charm now