<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Deceiving Arts</title>
	<atom:link href="http://deceivingarts.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://deceivingarts.com/blog</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Mon, 14 Feb 2011 03:02:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Watch out how you format that navigation string</title>
		<link>http://deceivingarts.com/blog/2011/02/watch-out-how-you-format-that-navigation-string/</link>
		<comments>http://deceivingarts.com/blog/2011/02/watch-out-how-you-format-that-navigation-string/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 02:48:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

		<guid isPermaLink="false">http://deceivingarts.com/blog/2011/02/watch-out-how-you-format-that-navigation-string/</guid>
		<description><![CDATA[Related Platforms: Silverlight and Windows Phone 7 The Problem I’ve noticed that the constructor on my MainPage was being called again, when I navigated back to the MainPage using the NavigationService. NavigationService.Navigate("/Views/MainPage.xaml"); .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Related Platforms: Silverlight and Windows Phone 7</strong></p>
<p><strong>The Problem</strong></p>
<p>I’ve noticed that the constructor on my MainPage was being called again, when I navigated back to the MainPage using the NavigationService. </p>
<div class="csharpcode">
<pre class="alt">NavigationService.Navigate(<span class="str">"/Views/MainPage.xaml"</span>);</pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>This manifested itself, by events getting fired multiple times, which is undesirable on multiple levels. Events being called multiple times can cause unnecessary web calls, more cpu and memory usage, thus leading to more battery usage and potentially a bigger bill for the user. </p>
<p><strong>Background</strong></p>
<p>I didn’t understand, why MainPage constructor would be called twice. I’ve read in multiple sources that your MainPage survives as long as the app does. Then I stumbled in to a post, which shed some light to the issue. If you navigate to the same page, but using a different query parameters, then a new version of that page is instantiated again. More details on this issue can be found here: <a href="http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/90f6a71d-a856-4b00-afe6-97e67b411417">http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/90f6a71d-a856-4b00-afe6-97e67b411417</a></p>
<p>This is marginally okay behavior, if you’ve recreated the ViewModel for each instance of a page aka Satellite ViewModels. However, you may have noticed that in the default project template, your MainViewModel is statically associated to your App.xaml. For more information on this, I highly recommend reading all three articles of this series: <a href="http://www.imaginativeuniversal.com/blog/post/2010/08/15/Patterns-of-Windows-Phone-Architecture-Part-1.aspx">http://www.imaginativeuniversal.com/blog/post/2010/08/15/Patterns-of-Windows-Phone-Architecture-Part-1.aspx</a></p>
<p><strong>Solution 1</strong></p>
<p>So great, then a solution would be to not use navigation query parameters, when navigating to your MainPage. You can use variables on your MainViewModel to change the behavior as you like.</p>
<pre class="csharpcode"><span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnNavigatedTo(NavigationEventArgs e)
{
      <span class="kwrd">base</span>.OnNavigatedTo(e);
      <span class="kwrd">if</span> (App.ViewModel.DoSomething)
      {
          App.ViewModel.DoSomething = <span class="kwrd">false</span>;
          DoSomething();
      }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p><strong>Problem 2</strong></p>
<p>You’d think this would’ve solved the issue and I could go to bed feeling good about myself. However, I was sorely mistaken. The constructor was still being called twice. Then it hit me, it wasn’t only a query parameter included in to the navigation string, but also the <em>exact</em> formatting of the navigation string.</p>
<p>The culprit in this case was WMAppManifest.xml. The NavigationPage was defined as shown, below:</p>
<pre class="csharpcode">&lt;Tasks&gt;
    &lt;DefaultTask Name=<span class="str">"_default"</span> NavigationPage=<span class="str">"Views\MainPage.xaml"</span> /&gt;
&lt;/Tasks&gt;</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>However, in my code, I was calling it like “/Views/MainPage.xaml”. For some reason Silverlight thinks “/Views/MainPage.xaml” and “/Views\MainPage.xaml” are different, so that’s why it was creating a separate instance of MainPage.</p>
<p><strong>Solution 2</strong></p>
<p>Watch out how you format that navigation string and be consistent.</p>
]]></content:encoded>
			<wfw:commentRss>http://deceivingarts.com/blog/2011/02/watch-out-how-you-format-that-navigation-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deceiving Arts blog goes live</title>
		<link>http://deceivingarts.com/blog/2011/01/deceiving-arts-blog-goes-live/</link>
		<comments>http://deceivingarts.com/blog/2011/01/deceiving-arts-blog-goes-live/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 01:35:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://deceivingarts.com/blog/2011/01/deceiving-arts-blog-goes-live/</guid>
		<description><![CDATA[Finally, it’s great to get a blog going, amidst concerns of slow performance with the hosting company. Please feel free to comment about any technical issues, you may experience with the blog. We’ll work to get any issues ironed out. Currently, we’re working hard to get our first Windows Phone 7 phone app out soon. [...]]]></description>
			<content:encoded><![CDATA[<p>Finally, it’s great to get a blog going, amidst concerns of slow performance with the hosting company. </p>
<p>Please feel free to comment about any technical issues, you may experience with the blog. We’ll work to get any issues ironed out.</p>
<p>Currently, we’re working hard to get our first Windows Phone 7 phone app out soon. Updates about the app and a new website will follow.</p>
]]></content:encoded>
			<wfw:commentRss>http://deceivingarts.com/blog/2011/01/deceiving-arts-blog-goes-live/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

