<?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/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Metro &#8211; WiredPrairie</title>
	<atom:link href="blog/archives/tag/metro/feed" rel="self" type="application/rss+xml" />
	<link>/blog</link>
	<description>Yet another tech blog.</description>
	<lastBuildDate>Wed, 29 Aug 2012 01:20:36 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0</generator>
<site xmlns="com-wordpress:feed-additions:1">193486638</site>	<item>
		<title>One way to find all Metro/Windows 8 Modern UI applications in C#</title>
		<link>/blog/index.php/archives/1723</link>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Wed, 29 Aug 2012 01:20:35 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Metro]]></category>
		<guid isPermaLink="false">/blog/?p=1723</guid>

					<description><![CDATA[Running this code as an administrator, you can use the following snippet as a method for determining the nature of a process on Windows 8 and whether it would seem that the running process is running in the new Modern UI shell (metro). static void Main(string[] args) { var allProcesses = Process.GetProcesses().Where(p =&#62; { try [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Running this code as an administrator, you can use the following snippet as a method for determining the nature of a process on Windows 8 and whether it would seem that the running process is running in the new Modern UI shell (metro).</p>
<pre class="code"><span style="background: white; color: blue">static void </span><span style="background: white; color: black">Main(</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">[] args)
{            
    </span><span style="background: white; color: blue">var </span><span style="background: white; color: black">allProcesses = </span><span style="background: white; color: #2b91af">Process</span><span style="background: white; color: black">.GetProcesses().Where(p =&gt;
    {
        </span><span style="background: white; color: blue">try
        </span><span style="background: white; color: black">{
            </span><span style="background: white; color: blue">var </span><span style="background: white; color: black">pid = p.Id;
            </span><span style="background: white; color: blue">var </span><span style="background: white; color: black">modules = p.Modules.Cast&lt;</span><span style="background: white; color: #2b91af">ProcessModule</span><span style="background: white; color: black">&gt;()
                .Where(m =&gt; m.FileName.Contains(</span><span style="background: white; color: #a31515">&quot;System.Runtime.WindowsRuntime&quot;</span><span style="background: white; color: black">));
            </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">modules.Count() &gt; 0;
        }
        </span><span style="background: white; color: blue">catch </span><span style="background: white; color: black">{ </span><span style="background: white; color: blue">return false</span><span style="background: white; color: black">; }
    }).OrderBy(p =&gt; p.MainModule.FileName).ToList();
    
    </span><span style="background: white; color: blue">for </span><span style="background: white; color: black">(</span><span style="background: white; color: blue">int </span><span style="background: white; color: black">i = 0; i &lt; allProcesses.Count(); i++)
    {
        </span><span style="background: white; color: blue">var </span><span style="background: white; color: black">p = allProcesses[i];
        </span><span style="background: white; color: #2b91af">Console</span><span style="background: white; color: black">.WriteLine(</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">.Format(</span><span style="background: white; color: #a31515">&quot;{0}. {1}&quot;</span><span style="background: white; color: black">, i, p.MainModule.FileName));
    }
    </span><span style="background: white; color: #2b91af">Console</span><span style="background: white; color: black">.ReadKey();
}</span></pre>
<p>Modern UI / Metro applications are protected and cannot be easily interrogated by a non-administrative process. While you can get some basics about all processes, a standard user process isn’t allowed to look at the loaded modules for example.</p>
<p>In the code above, all processes are scanned for a particular DLL. In this case, System.Runtime.WindowsRuntime. I’m not 100% confident this is the best choice … there may be a few better options (or multiple that are required). (If you know of them, please leave a comment!). It did find the Modern UI / Metro applications running in my Windows 8 VM. </p>
<p>Once gathered, the code just outputs the basics to the console. (The name of the host, which is WWAHost.exe apparently some times).</p>
<p>Next step is to learn something useful via the process object.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1723</post-id>	</item>
		<item>
		<title>WinRT/Xaml/AKA Metro DataTemplate selection based on Data Types</title>
		<link>/blog/index.php/archives/1705</link>
					<comments>/blog/index.php/archives/1705#comments</comments>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Tue, 21 Aug 2012 01:02:03 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Metro]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows8]]></category>
		<category><![CDATA[Xaml]]></category>
		<guid isPermaLink="false">/blog/?p=1705</guid>

					<description><![CDATA[You may have noticed that WinRT does not have automatic resolution of a DataTemplate based on the data type of object added to an ItemsControl. While unfortunate as this behavior is quite handy, it’s not too difficult to replicate the functionality using a DataTemplateSelector. WPF for example, could do something like this: &#60;DataTemplate DataType=&#34;{x:Type local:Task}&#34;&#62; [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>You may have noticed that WinRT does not have automatic resolution of a DataTemplate based on the data type of object added to an <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.itemscontrol.aspx" target="_blank">ItemsControl</a>. While unfortunate as this behavior is quite handy, it’s not too difficult to replicate the functionality using a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.datatemplateselector" target="_blank">DataTemplateSelector</a>.</p>
<p>WPF for example, could do something like this: </p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">&lt;DataTemplate DataType=<span style="color: #006080">&quot;{x:Type local:Task}&quot;</span>&gt;<br />  &lt;StackPanel&gt;<br />    &lt;TextBlock Text=<span style="color: #006080">&quot;{Binding Path=TaskName}&quot;</span> /&gt;<br />    &lt;TextBlock Text=<span style="color: #006080">&quot;{Binding Path=Description}&quot;</span>/&gt;<br />    &lt;TextBlock Text=<span style="color: #006080">&quot;{Binding Path=Priority}&quot;</span>/&gt;<br />  &lt;/StackPanel&gt;<br />&lt;/DataTemplate&gt;</pre>
<p></div>
<p>When the Task type as shown above was found in a list, it would have been rendered as a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.stackpanel.aspx" target="_blank">StackPanel</a> with three <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textblock.aspx" target="_blank">TextBlock</a>s automatically. That rocked.</p>
<p>WinRT (Metro/Xaml, Windows 8 applications) are missing the DataType property of DataTemplates. Yes, some of you might say it’s not missing as it’s V1, but given the heritage of Windows 8 Xaml applications, I consider it missing.</p>
<p>While an exact duplicate of the functionality isn’t possible, it’s relatively simple to get close.</p>
<pre class="code"><span style="background: white; color: blue">&lt;</span><span style="background: white; color: #a31515">GridView
    </span><span style="background: white; color: red">x</span><span style="background: white; color: blue">:</span><span style="background: white; color: red">Name</span><span style="background: white; color: blue">=&quot;itemGridView&quot;
</span><span style="background: white; color: blue">    </span><span style="background: white; color: red">ItemsSource</span><span style="background: white; color: blue">=&quot;{</span><span style="background: white; color: #a31515">Binding </span><span style="background: white; color: red">Source</span><span style="background: white; color: blue">={</span><span style="background: white; color: #a31515">StaticResource </span><span style="background: white; color: red">groupedItemsViewSource</span><span style="background: white; color: blue">}}&quot;
    </span><span style="background: white; color: red">ItemTemplateSelector</span><span style="background: white; color: blue">=&quot;{</span><span style="background: white; color: #a31515">StaticResource </span><span style="background: white; color: red">typedTemplateSelector</span><span style="background: white; color: blue">}&quot;
    </span><span style="background: white; color: red">SelectionMode</span><span style="background: white; color: blue">=&quot;None&quot;
    </span><span style="background: white; color: red">IsItemClickEnabled</span><span style="background: white; color: blue">=&quot;True&quot;</span><span style="background: white; color: blue">&gt;</span></pre>
<p>Take the GridView above for example (using the template project in Visual Studio 2012).</p>
<p>I’ve assigned the ItemTemplateSelector to an instance of the TypedTemplateSelector class I’ve created.</p>
<p>In the Resources for the Page, I added a custom DataTemplate:</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/08/image.png" width="323" height="186" /></p>
<p>I’ve added a DataTemplate with a Key called <strong>Type : SampleDataItem</strong> (without the spaces though – they’re auto converted by my WordPress theme to a squiggle face: <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-confusedsmile" alt="Confused smile" src="blog/wpcontent/uploads/2012/08/wlEmoticon-confusedsmile.png" />).</p>
<p>There’s nothing special about the Template, just the name. It must start with <strong>Type:.</strong></p>
<p>Here the custom template selector is being constructed in the Resources:</p>
<pre class="code"><span style="background: white; color: black">    </span><span style="background: white; color: blue">&lt;</span><span style="background: white; color: #a31515">local</span><span style="background: white; color: blue">:</span><span style="background: white; color: #a31515">TypedTemplateSelector </span><span style="background: white; color: red">x</span><span style="background: white; color: blue">:</span><span style="background: white; color: red">Key</span><span style="background: white; color: blue">=&quot;typedTemplateSelector&quot;
                                 </span><span style="background: white; color: red">DefaultTemplateKey</span><span style="background: white; color: blue">=&quot;Standard250x250ItemTemplate&quot; /&gt;
&lt;/</span><span style="background: white; color: #a31515">Page.Resources</span><span style="background: white; color: blue">&gt;</span></pre>
<p>Here’s the class that you’d need below. It has a caching feature (<strong>IsCacheEnabled</strong>) to prevent the search from occurring more than once for a key. It’s set to True by default. When used, the object searches from the current Item through all Parents trying to match the type of the object (via the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.visualtreehelper.aspx" target="_blank">VisualTreeHelper</a>). Only the ClassName is used as programmed below (and not the full namespace). You could easily change this behavior by removing the <strong>Split </strong>and <strong>Last </strong>calls.</p>
<pre class="code"><span style="background: white; color: blue">using </span><span style="background: white; color: black">System;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">System.Collections.Generic;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">System.Linq;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">System.Text;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">System.Threading.Tasks;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">Windows.UI.Xaml;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">Windows.UI.Xaml.Controls;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">Windows.UI.Xaml.Media;

</span><span style="background: white; color: blue">namespace </span><span style="background: white; color: black">WiredPrairie.TemplateSelector
{
    </span><span style="background: white; color: blue">public class </span><span style="background: white; color: #2b91af">TypedTemplateSelector </span><span style="background: white; color: black">: </span><span style="background: white; color: #2b91af">DataTemplateSelector
    </span><span style="background: white; color: black">{
        </span><span style="background: white; color: blue">private </span><span style="background: white; color: #2b91af">Dictionary</span><span style="background: white; color: black">&lt;</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">, </span><span style="background: white; color: #2b91af">DataTemplate</span><span style="background: white; color: black">&gt; _cachedDataTemplates;

        </span><span style="background: white; color: gray">/// &lt;summary&gt;
        /// </span><span style="background: white; color: green">Fallback value for DataTemplate
        </span><span style="background: white; color: gray">/// &lt;/summary&gt;
        </span><span style="background: white; color: blue">public string </span><span style="background: white; color: black">DefaultTemplateKey { </span><span style="background: white; color: blue">get</span><span style="background: white; color: black">; </span><span style="background: white; color: blue">set</span><span style="background: white; color: black">; }

        </span><span style="background: white; color: gray">/// &lt;summary&gt;
        /// </span><span style="background: white; color: green">Cache search results for a type (defaults to Enabled)
        </span><span style="background: white; color: gray">/// &lt;/summary&gt;
        </span><span style="background: white; color: blue">public bool </span><span style="background: white; color: black">IsCacheEnabled { </span><span style="background: white; color: blue">get</span><span style="background: white; color: black">; </span><span style="background: white; color: blue">set</span><span style="background: white; color: black">; }

        </span><span style="background: white; color: blue">public </span><span style="background: white; color: black">TypedTemplateSelector()
        {
            IsCacheEnabled = </span><span style="background: white; color: blue">true</span><span style="background: white; color: black">;
        }

        </span><span style="background: white; color: blue">protected override </span><span style="background: white; color: black">Windows.UI.Xaml.</span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">SelectTemplateCore(</span><span style="background: white; color: blue">object </span><span style="background: white; color: black">item, Windows.UI.Xaml.</span><span style="background: white; color: #2b91af">DependencyObject </span><span style="background: white; color: black">container)
        {
            </span><span style="background: white; color: green">// grab the Type name. Type will be searched as Type:NAME as shown below
            /*
                &lt;DataTemplate x:Key=&quot;Type:SampleDataItem&quot;&gt;
                    &lt;Grid HorizontalAlignment=&quot;Left&quot; Width=&quot;250&quot; Height=&quot;250&quot;&gt;
                        &lt;TextBlock Text=&quot;{Binding Title}&quot; /&gt;
                    &lt;/Grid&gt;
                &lt;/DataTemplate&gt;
             */
            </span><span style="background: white; color: blue">string </span><span style="background: white; color: black">key = item != </span><span style="background: white; color: blue">null </span><span style="background: white; color: black">? </span><span style="background: white; color: blue">string</span><span style="background: white; color: black">.Format(</span><span style="background: white; color: #a31515">&quot;Type:{0}&quot;</span><span style="background: white; color: black">, item.GetType().Name.Split(</span><span style="background: white; color: #a31515">'.'</span><span style="background: white; color: black">).Last()) : DefaultTemplateKey;
            </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">dt = GetCachedDataTemplate(key);
            </span><span style="background: white; color: blue">try
            </span><span style="background: white; color: black">{
                </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(dt != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">) { </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">dt; }

                </span><span style="background: white; color: green">// look at all parents (visual parents)
                </span><span style="background: white; color: #2b91af">FrameworkElement </span><span style="background: white; color: black">fe = container </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">FrameworkElement</span><span style="background: white; color: black">;
                </span><span style="background: white; color: blue">while </span><span style="background: white; color: black">(fe != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
                {
                    dt = FindTemplate(fe, key);
                    </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(dt != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">) { </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">dt; }
                    </span><span style="background: white; color: green">// if you were to just look at logical parents,
                    // you'd find that there isn't a Parent for Items set
                    </span><span style="background: white; color: black">fe = </span><span style="background: white; color: #2b91af">VisualTreeHelper</span><span style="background: white; color: black">.GetParent(fe) </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">FrameworkElement</span><span style="background: white; color: black">;
                }

                dt = FindTemplate(</span><span style="background: white; color: blue">null</span><span style="background: white; color: black">, key);
                </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">dt;
            }
            </span><span style="background: white; color: blue">finally
            </span><span style="background: white; color: black">{
                </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(dt != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
                {
                    AddCachedDataTemplate(key, dt);
                }
            }
        }

        </span><span style="background: white; color: blue">private </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">GetCachedDataTemplate(</span><span style="background: white; color: blue">string </span><span style="background: white; color: black">key)
        {
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(!IsCacheEnabled) { </span><span style="background: white; color: blue">return null</span><span style="background: white; color: black">; }
            VerifyCachedDataTemplateStorage();
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(_cachedDataTemplates.ContainsKey(key))
            {
                </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">_cachedDataTemplates[key];
            }

            </span><span style="background: white; color: blue">return null</span><span style="background: white; color: black">;
        }

        </span><span style="background: white; color: blue">private void </span><span style="background: white; color: black">AddCachedDataTemplate(</span><span style="background: white; color: blue">string </span><span style="background: white; color: black">key, </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">dt)
        {
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(!IsCacheEnabled) { </span><span style="background: white; color: blue">return</span><span style="background: white; color: black">; }
            VerifyCachedDataTemplateStorage();
            _cachedDataTemplates[key] = dt;
        }

        </span><span style="background: white; color: gray">/// &lt;summary&gt;
        /// </span><span style="background: white; color: green">Delay creates storage
        </span><span style="background: white; color: gray">/// &lt;/summary&gt;
        </span><span style="background: white; color: blue">private void </span><span style="background: white; color: black">VerifyCachedDataTemplateStorage()
        {
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(_cachedDataTemplates == </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
            {
                _cachedDataTemplates = </span><span style="background: white; color: blue">new </span><span style="background: white; color: #2b91af">Dictionary</span><span style="background: white; color: black">&lt;</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">, </span><span style="background: white; color: #2b91af">DataTemplate</span><span style="background: white; color: black">&gt;();
            }

        }

        </span><span style="background: white; color: gray">/// &lt;summary&gt;
        /// </span><span style="background: white; color: green">Returns a template
        </span><span style="background: white; color: gray">/// &lt;/summary&gt;
        /// &lt;param name=&quot;source&quot;&gt;</span><span style="background: white; color: green">Pass null to search entire app</span><span style="background: white; color: gray">&lt;/param&gt;
        /// &lt;param name=&quot;key&quot;&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        </span><span style="background: white; color: blue">private static </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">FindTemplate(</span><span style="background: white; color: blue">object </span><span style="background: white; color: black">source, </span><span style="background: white; color: blue">string </span><span style="background: white; color: black">key)
        {
            </span><span style="background: white; color: blue">var </span><span style="background: white; color: black">fe = source </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">FrameworkElement</span><span style="background: white; color: black">;
            </span><span style="background: white; color: blue">object </span><span style="background: white; color: black">obj;
            </span><span style="background: white; color: #2b91af">ResourceDictionary </span><span style="background: white; color: black">rd = fe != </span><span style="background: white; color: blue">null </span><span style="background: white; color: black">? fe.Resources : </span><span style="background: white; color: #2b91af">App</span><span style="background: white; color: black">.Current.Resources;
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(rd.TryGetValue(key, </span><span style="background: white; color: blue">out </span><span style="background: white; color: black">obj))
            {
                </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">dt = obj </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">DataTemplate</span><span style="background: white; color: black">;
                </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(dt != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
                {
                    </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">dt;
                }
            }
            </span><span style="background: white; color: blue">return null</span><span style="background: white; color: black">;

        }
    }
}
</span></pre>
]]></content:encoded>
					
					<wfw:commentRss>/blog/index.php/archives/1705/feed</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1705</post-id>	</item>
		<item>
		<title>Windows 8 WinRT/Metro Missing UpdateSourceTrigger</title>
		<link>/blog/index.php/archives/1701</link>
					<comments>/blog/index.php/archives/1701#comments</comments>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Sun, 05 Aug 2012 20:53:15 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Metro]]></category>
		<category><![CDATA[WinRT]]></category>
		<guid isPermaLink="false">/blog/?p=1701</guid>

					<description><![CDATA[If you’ve done WPF or Silverlight programming, you may have found an occasion where using the Binding property UpdateSourceTrigger set to PropertyChanged was extremely useful. (I know I have!) It may have looked something like this: &#60;TextBox Text=&#34;{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}&#34; /&#62; The key feature was the live updating of the property through the binding. For [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>If you’ve done WPF or Silverlight programming, you may have found an occasion where using the Binding property <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger(v=vs.110).aspx" target="_blank">UpdateSourceTrigger</a> set to PropertyChanged was extremely useful. (I know I have!)</p>
<p>It may have looked something like this:</p>
<pre class="code"><span style="background: white; color: blue">&lt;</span><span style="background: white; color: #a31515">TextBox </span><span style="background: white; color: red">Text</span><span style="background: white; color: blue">=</span><span style="background: white; color: black">&quot;</span><span style="background: white; color: blue">{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}</span><span style="background: white; color: black">&quot; </span><span style="background: white; color: blue">/&gt;</span></pre>
<p>The key feature was the live updating of the property through the binding. For example, as the user typed in the TextBox above, the property <strong>Value</strong> in the bound object would have been updated as the user typed. The default behavior in Silverlight 5 and WPF is to use LostFocus: the <strong>Value</strong> is only updated when the TextBox loses focus. Sometimes, that’s OK. </p>
<p>In WinRT/Metro however, this option was completely removed, and no decent replacement was provided unfortunately. In searching for a reasonable work-around, I discovered a few other related missing pieces from WinRT/XAML:</p>
<ul>
<li>BindingOptions.<a href="http://msdn.microsoft.com/en-us/library/system.windows.data.bindingoperations.getbinding(v=vs.110).aspx" target="_blank">GetBinding</a>: This allowed the WPF programmer a method for retrieving a Binding object. I was interested in this option as I wanted to clone the original Binding, make it two-way, and attach to the TextChanged event so that a new binding could be updated when the text changed, with the new binding pointing to the same property as the Text property. (My solution uses the basic idea as this).</li>
<li>FrameworkElement.<a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.getbindingexpression(v=vs.110).aspx" target="_blank">GetBindingExpression</a>: Again, gone. Same basic logic as above. (Probably uses same underlying support that just isn’t there).</li>
<li>XamlWriter.Save: XamlWriter is completely gone. I thought maybe I’d save an instance of the TextBox to Xaml, and create a new one with adjusted bindings from the Xaml. XamlReader does exist.</li>
</ul>
<p>You can start to feel how Xaml really is just a thin wrapper on the Windows 8 run time as you start to look around to see what remains from WPF. Even traditional Silverlight features are gone unfortunately. Maybe Windows 9? <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="blog/wpcontent/uploads/2012/08/wlEmoticon-smile.png" /></p>
<p>So, I took a round-about approach to solving this problem. </p>
<pre class="code"><span style="background: white; color: black"> </span><span style="background: white; color: blue">public class </span><span style="background: white; color: #2b91af">UpdateSourceHelper </span><span style="background: white; color: black">: </span><span style="background: white; color: #2b91af">FrameworkElement
 </span><span style="background: white; color: black">{
     </span><span style="background: white; color: blue">public static string </span><span style="background: white; color: black">GetUpdateSourceText(</span><span style="background: white; color: #2b91af">DependencyObject </span><span style="background: white; color: black">obj)
     {
         </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">(</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">)obj.GetValue(UpdateSourceTextProperty);
     }

     </span><span style="background: white; color: blue">public static void </span><span style="background: white; color: black">SetUpdateSourceText(</span><span style="background: white; color: #2b91af">DependencyObject </span><span style="background: white; color: black">obj, </span><span style="background: white; color: blue">string </span><span style="background: white; color: black">value)
     {
         obj.SetValue(UpdateSourceTextProperty, value);
     }

     </span><span style="background: white; color: green">// Using a DependencyProperty as the backing store for UpdateSourceText.  This enables animation, styling, binding, etc...
     </span><span style="background: white; color: blue">public static readonly </span><span style="background: white; color: #2b91af">DependencyProperty </span><span style="background: white; color: black">UpdateSourceTextProperty =
         </span><span style="background: white; color: #2b91af">DependencyProperty</span><span style="background: white; color: black">.RegisterAttached(</span><span style="background: white; color: #a31515">&quot;UpdateSourceText&quot;</span><span style="background: white; color: black">, </span><span style="background: white; color: blue">typeof</span><span style="background: white; color: black">(</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">), </span><span style="background: white; color: blue">typeof</span><span style="background: white; color: black">(</span><span style="background: white; color: #2b91af">UpdateSourceHelper</span><span style="background: white; color: black">), </span><span style="background: white; color: blue">new </span><span style="background: white; color: #2b91af">PropertyMetadata</span><span style="background: white; color: black">(</span><span style="background: white; color: #a31515">&quot;&quot;</span><span style="background: white; color: black">));
     
     </span><span style="background: white; color: blue">public static bool </span><span style="background: white; color: black">GetIsEnabled(</span><span style="background: white; color: #2b91af">DependencyObject </span><span style="background: white; color: black">obj)
     {
         </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">(</span><span style="background: white; color: blue">bool</span><span style="background: white; color: black">)obj.GetValue(IsEnabledProperty);
     }

     </span><span style="background: white; color: blue">public static void </span><span style="background: white; color: black">SetIsEnabled(</span><span style="background: white; color: #2b91af">DependencyObject </span><span style="background: white; color: black">obj, </span><span style="background: white; color: blue">bool </span><span style="background: white; color: black">value)
     {
         obj.SetValue(IsEnabledProperty, value);
     }

     </span><span style="background: white; color: green">// Using a DependencyProperty as the backing store for IsEnabled.  This enables animation, styling, binding, etc...
     </span><span style="background: white; color: blue">public static readonly </span><span style="background: white; color: #2b91af">DependencyProperty </span><span style="background: white; color: black">IsEnabledProperty =
         </span><span style="background: white; color: #2b91af">DependencyProperty</span><span style="background: white; color: black">.RegisterAttached(</span><span style="background: white; color: #a31515">&quot;IsEnabled&quot;</span><span style="background: white; color: black">, </span><span style="background: white; color: blue">typeof</span><span style="background: white; color: black">(</span><span style="background: white; color: blue">bool</span><span style="background: white; color: black">), </span><span style="background: white; color: blue">typeof</span><span style="background: white; color: black">(</span><span style="background: white; color: #2b91af">UpdateSourceHelper</span><span style="background: white; color: black">), 
             </span><span style="background: white; color: blue">new </span><span style="background: white; color: #2b91af">PropertyMetadata</span><span style="background: white; color: black">(</span><span style="background: white; color: blue">false</span><span style="background: white; color: black">, 
             </span><span style="background: white; color: green">// property changed
             </span><span style="background: white; color: black">(obj, args)=&gt; 
             {                    
                 </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(obj </span><span style="background: white; color: blue">is </span><span style="background: white; color: #2b91af">TextBox</span><span style="background: white; color: black">)
                 {
                     </span><span style="background: white; color: #2b91af">TextBox </span><span style="background: white; color: black">tb = (</span><span style="background: white; color: #2b91af">TextBox</span><span style="background: white; color: black">)obj;
                     </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">((</span><span style="background: white; color: blue">bool</span><span style="background: white; color: black">)args.NewValue)
                     {                                                                                      
                         tb.TextChanged += AttachedTextBoxTextChanged;
                     }
                     </span><span style="background: white; color: blue">else
                     </span><span style="background: white; color: black">{
                         tb.TextChanged -= AttachedTextBoxTextChanged;
                     }
                 }
             }
         ));

     </span><span style="background: white; color: blue">static void </span><span style="background: white; color: black">AttachedTextBoxTextChanged(</span><span style="background: white; color: blue">object </span><span style="background: white; color: black">sender, </span><span style="background: white; color: #2b91af">TextChangedEventArgs </span><span style="background: white; color: black">e)
     {
         </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(sender </span><span style="background: white; color: blue">is </span><span style="background: white; color: #2b91af">TextBox</span><span style="background: white; color: black">)
         {
             </span><span style="background: white; color: blue">var </span><span style="background: white; color: black">tb = (</span><span style="background: white; color: #2b91af">TextBox</span><span style="background: white; color: black">)sender;
             tb.SetValue(</span><span style="background: white; color: #2b91af">UpdateSourceHelper</span><span style="background: white; color: black">.UpdateSourceTextProperty, tb.Text);                
         }
     }        
 }</span></pre>
<p>And here it is in use:</p>
<pre class="code"><span style="background: white; color: red">xmlns</span><span style="background: white; color: blue">:</span><span style="background: white; color: red">local</span><span style="background: white; color: blue">=&quot;using:WiredPrairie.Converter&quot;</span></pre>
<p>(The above namespace is needed to provide context to the Xaml parser for the attached properties. Change it to whatever you need).</p>
<pre class="code"><span style="background: white; color: blue">&lt;</span><span style="background: white; color: #a31515">TextBox </span><span style="background: white; color: red">Height</span><span style="background: white; color: blue">=&quot;Auto&quot; </span><span style="background: white; color: red">Margin</span><span style="background: white; color: blue">=&quot;0,6&quot; </span><span style="background: white; color: red">Grid.Row</span><span style="background: white; color: blue">=&quot;1&quot; </span><span style="background: white; color: red">TextWrapping</span><span style="background: white; color: blue">=&quot;Wrap&quot; </span><span style="background: white; color: red">TabIndex</span><span style="background: white; color: blue">=&quot;0&quot; 
         </span><span style="background: white; color: red">Text</span><span style="background: white; color: blue">=&quot;{</span><span style="background: white; color: #a31515">Binding </span><span style="background: white; color: red">Value</span><span style="background: white; color: blue">}&quot; 
         </span><span style="background: white; color: red">local</span><span style="background: white; color: blue">:</span><span style="background: white; color: red">UpdateSourceHelper.IsEnabled</span><span style="background: white; color: blue">=&quot;True&quot; 
         </span><span style="background: white; color: red">local</span><span style="background: white; color: blue">:</span><span style="background: white; color: red">UpdateSourceHelper.UpdateSourceText</span><span style="background: white; color: blue">=&quot;{</span><span style="background: white; color: #a31515">Binding </span><span style="background: white; color: red">Value</span><span style="background: white; color: blue">, </span><span style="background: white; color: red">Mode</span><span style="background: white; color: blue">=TwoWay}&quot;/&gt;</span></pre>
<p>A classic pattern in WPF for extending built in controls without subclassing was to create an attached behavior. Thankfully, this pattern still works.</p>
<p>The core concept is to attach to the original instance of the TextBox by using an enabling property. In this case, an <a href="http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&amp;l=EN-US&amp;k=k(Windows.UI.Xaml.DependencyProperty.RegisterAttached);k(TargetFrameworkMoniker-.NETCore,Version%3Dv4.5);k(DevLang-csharp)&amp;rd=true" target="_blank">attached property</a>&#160; called IsEnabled is added to the TextBox.</p>
<p>When the value of the IsEnabled property changes from the default of false, the <a href="http://msdn.microsoft.com/en-us/library/windows.ui.xaml.propertychangedcallback.aspx" target="_blank">PropertyChanged</a> callback is called. It’s here that the simple magic happens. As long as the attached property is attached to an instance of TextBox, it wires up to the TextChanged event. </p>
<p>In the TextChanged event handler, the value of the attached TextBox (Text property) is set into the second binding’s value. By doing this, it then sets the datasource’s object’s bound property to the new value immediately. </p>
<p>The annoying part about this solution really is the extra binding that is required. As there isn’t a documented/known API for reading an existing binding, this technique requires that the developer specify the binding <strong><em>twice</em></strong>. </p>
<p><span style="background: white; color: red">local</span><span style="background: white; color: blue">:</span><span style="background: white; color: red">UpdateSourceHelper.UpdateSourceText</span><span style="background: white; color: blue">=&quot;{</span><span style="background: white; color: #a31515">Binding </span><span style="background: white; color: red">Value</span><span style="background: white; color: blue">, </span><span style="background: white; color: red">Mode</span><span style="background: white; color: blue">=TwoWay}&quot;</span></p>
<p>In WPF, it would have been possible to declare the UpdateSourceText as defaulting to TwoWay binding. But, again, that feature has been removed. Without the TwoWay binding mode, the SetValue call in the TextChanged event handler will not update the object’s bound property. It’s one way.</p>
<p>I looked low and high for a more elegant solution, but couldn’t find one. If you find something better, please leave a comment!</p>
]]></content:encoded>
					
					<wfw:commentRss>/blog/index.php/archives/1701/feed</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1701</post-id>	</item>
		<item>
		<title>A bit of reflow, using TweenJS</title>
		<link>/blog/index.php/archives/1664</link>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Sun, 13 May 2012 16:59:54 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Metro]]></category>
		<category><![CDATA[TweenJS]]></category>
		<guid isPermaLink="false">/blog/?p=1664</guid>

					<description><![CDATA[Inspired by some modern applications (especially the “Metro” look and feel), I wanted to put together an animated tile reflow script for an HTML page I was creating. As the size of the page changes, the tiles move into their new positions. You can try it here, if you’re using a modern browser (IE9+, Chrome, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Inspired by some modern applications (especially the “Metro” look and feel), I wanted to put together an animated tile reflow script for an HTML page I was creating. </p>
<p>As the size of the page changes, the tiles move into their new positions.</p>
<p><a href="http://www.wiredprairie.us/examples/easeljs/reflow1/" target="_blank"><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="SNAGHTML113b633f[4]" border="0" alt="SNAGHTML113b633f[4]" src="blog/wpcontent/uploads/2012/05/SNAGHTML113b633f4.png" width="502" height="408" /></a></p>
<p>You can try it <a href="http://www.wiredprairie.us/examples/easeljs/reflow1/" target="_blank">here</a>, if you’re using a modern browser (IE9+, Chrome, FF, Safari, etc.).</p>
<p>Dependencies:</p>
<ol>
<li><a href="http://createjs.com/#!/TweenJS" target="_blank">TweenJS</a></li>
<li><a href="http://createjs.com/#!/EaselJS" target="_blank">EaselJS</a> (for the ticker used in TweenJS)</li>
<li>CSSPlugin (an add on to the the TweenJS library)</li>
<li><a href="http://www.jquery.com/" target="_blank">jQuery</a> (1.7.2)</li>
</ol>
<p> Relayouts are queued (as to prevent multiple from running).</p>
<p>Only boxes that will be visible to the end user (either animating to the screen or off the screen) are animated. It does not animate elements that do not start or end in the current viewport including boxes that would animate “through” the current viewport. I considered the latter to not add anything to the overall experience and just uses more CPU to perform the animations, so I intentionally left it out.</p>
<p>In the layout loop, the code verifies that the animation needs to run:</p>
<pre class="code"><span style="color: #006400">// here's the final destination
</span>pos = { left: Math.round(x), top: Math.round(y) };

<span style="color: #006400">// check old and new positions, if either are in the viewport, we'll animate
</span><span style="color: blue">if </span>(isScrolledIntoView(pos.top + parentOfBoxesOffsetTop, boxH) || isScrolledIntoView(box.y + parentOfBoxesOffsetTop, boxH)) {
    Tween.get(box.e).to(pos, 500 + 500 * Math.random(), 
        Ease.quadInOut).call(<span style="color: blue">function </span>() {
            --layoutPending;
            <span style="color: blue">if </span>(!layoutPending &amp;&amp; queuedLayout) { queueLayout(); }
    });
} <span style="color: blue">else </span>{</pre>
<p>If it needs to animate, the code uses the static Tween method “get” to start an animation on the current element. </p>
<ol>
<li>Tween.get(box.e) creates a new instance of the Tween class (initialized with the element to tween).</li>
<li>Using that Tween instance, the CSS attributes (thanks to the handy CSSPlugin), for left and top are animated for at least 500ms and up to a full second. The easing function I chose is quadInOut. Tip: <a href="http://www.createjs.com/#!/TweenJS/demos/sparkTable" target="_blank">Here’s</a> a great way to visualize easing options using TweenJS.</li>
<li>Finally, when the animation completes, an anonymous function is called which decrements the number of pending animations and if there are no remaining elements to animate, and there’s something queued, another cycle is started.</li>
</ol>
<p>All the code used by the demo is in default.html.</p>
<p>Colors of the boxes were set using hsl (only available in modern browsers):</p>
<pre class="code">box.style.backgroundColor = <span style="color: maroon">&quot;hsl(&quot; </span>+ c + <span style="color: maroon">&quot;, 100%, 50%)&quot;</span>;</pre>
<p>Where “c” is:</p>
<pre class="code">c = (i / boxes * 360).toFixed(1);</pre>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1664</post-id>	</item>
	</channel>
</rss>
