<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>David Nissimoff's blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/davidni/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/davidni/feed.xml</link>
    <description />
    <lastBuildDate>Mon, 30 Apr 2018 23:01:53 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Reusable PowerApps Control Templates</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/reusable-powerapps-control-templates</link>
      <pubDate>Wed, 25 Apr 2018 12:37:10 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/?p=415</guid>
      <description><![CDATA[Does PowerApps support control templates? For example, imagine I am a company and I want a standard...]]></description>
      <content:encoded><![CDATA[<strong>Does PowerApps support control templates?</strong> For example, imagine I am a company and I want a standard header or footer in all PowerApps that my employees make. Or perhaps I built some non-trivial piece of functionality like a <a href="https://blogs.msdn.microsoft.com/davidni/2016/12/13/building-an-interactive-calendar-view-with-powerapps/" rel="noopener" target="_blank">calendar view</a> that I want to use in many apps. <strong>Can I avoid duplicated effort?</strong>

So far there have been a few partial solutions or workarounds for this. PowerApps recently added support for <a href="https://powerapps.microsoft.com/en-us/blog/copy-and-paste-controls-across-canvas-apps-available/" rel="noopener" target="_blank">copy / paste operations across tabs in PowerApps Web Studio</a> which enables you to open an app, copy controls, then paste onto a new app.

This blog post introduces a tool I wrote to make it easier to share portions of an app. I refer to these as <strong>control templates</strong>.

<h4><strong>Here it is: <a href="https://patemplates.azurewebsites.net/" rel="noopener" target="_blank">https://patemplates.azurewebsites.net/</a></strong></h4>

<a href="https://msdnshared.blob.core.windows.net/media/2018/04/davidnipatemplates2.gif"><img src="https://msdnshared.blob.core.windows.net/media/2018/04/davidnipatemplates2.gif" alt="" width="858" height="551" class="alignnone size-full wp-image-425" /></a>

<h2>Using a template</h2>
Say you want to add a calendar view like the one from my previous blog post <a href="https://blogs.msdn.microsoft.com/davidni/2016/12/13/building-an-interactive-calendar-view-with-powerapps/" rel="noopener" target="_blank">here</a>. I created a template for it, so now you can do it in 3 easy steps:

<ol>
  <li><strong>Open the template</strong>: <a href="https://patemplates.azurewebsites.net/templates/4b77bfd8-811e-4a41-88d3-c129b96f2d2d" rel="noopener" target="_blank">https://patemplates.azurewebsites.net/templates/4b77bfd8-811e-4a41-88d3-c129b96f2d2d</a></li>
  <li>When the page loads, <strong>press Ctrl+C</strong> (copy). The template is now copied to your clipboard</li>
  <li>In your app in Web Studio, <strong>press Ctrl+V</strong> (paste)</li>
</ol>

That's it! Compare this to having to either reproduce each step in that blog post, or having to download the sample app, copying several controls and formulas, etc.

<h2>Creating a template</h2>
Creating your own template is just as easy.
<ol>
  <li>In Web Studio, select the controls you want to templatize and <strong>press Ctrl+C</strong> (copy)</li>
  <li><strong>Open <a href="https://patemplates.azurewebsites.net" rel="noopener" target="_blank">https://patemplates.azurewebsites.net</a></strong> and <strong>press Ctrl+V</strong> (paste)</li>
  <li>Done! You will <strong>get a link</strong> to your new template.</li>
</ol>

<blockquote><strong>Note:</strong> your template will be stored and made available publicly to anyone who has the link. You should <strong>always assume any templates you create are public</strong> -- do not use this if you have sensitive information in your formulas.</blockquote>

Nice and easy. There are, however, limitations:

<ul>
  <li><strong>Data sources are not copied.</strong> If you copy controls that access external data, you will have to set up the data sources in order to use the templates correctly</li>
  <li><strong>Media are not copied.</strong> If your template includes an image / video / audio control with media that was embedded on your app, the media will not be part of the template</li>
  <li>You may have to recreate your templates from time to time as PowerApps releases new versions. If you start seeing issues, just open the app that has the controls you care about, and create a new template from them.</li>
  <li><strong>Only Edge and Chrome</strong> are supported.
</ul>

You might see warning signs on some formulas until you manually fix any issues caused by the above.

<h2>Wrapping up</h2>
I built this tool for myself as a PowerApps blog author -- now I can more easily share content for the community to consume. I hope that by sharing this tool others will also benefit. It should be obvious that this tool is not an official Microsoft product. While I intend to do my best to keep it running and operational, you shouldn't expect the same level of polish and reliability as PowerApps itself.

What do you think? Share your thoughts or feedback below!]]></content:encoded>
    </item>
    <item>
      <title>Building a calculator in PowerApps (that can do 1 + 2 + 3)</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/building-a-calculator-in-powerapps-that-can-do-1-2-3</link>
      <pubDate>Mon, 13 Nov 2017 01:14:33 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/?p=366</guid>
      <description><![CDATA[Given the recent news coverage of a remarkable bug in a very popular calculator app on a very...]]></description>
      <content:encoded><![CDATA[Given the recent news coverage of a remarkable bug in a very popular calculator app on a very popular mobile device (<a href="https://www.macrumors.com/2017/10/24/ios-11-calculator-animation-bug/" rel="noopener" target="_blank">[1]</a>, <a href="https://www.iphonehacks.com/2017/10/bug-prevents-ios-11-calculator-app-simple-calculation.html" rel="noopener" target="_blank">[2]</a>, <a href="https://www.ubergizmo.com/2017/10/ios-11-calculator-bug/" rel="noopener" target="_blank">[3]</a>), the question came up of how hard would it be to build a simple calculator using <a href="https://powerapps.microsoft.com/" rel="noopener" target="_blank">PowerApps</a>.

This article presents what I came up with. Obviously this is a gimmick more than anything else. But perhaps you can learn a thing or two from it. This calculator can behave in funny (but explainable) ways for numbers that are too large or too close to zero, and I am deliberately not attempting to handle such cases in order to keep things simple. This is not meant to replace a proper calculator, and as-is it <strong>can</strong> compute 1 + 2 + 3 = 6, so there you have it :)

<a href="https://msdnshared.blob.core.windows.net/media/2017/11/davidnicalc.gif"><img src="https://msdnshared.blob.core.windows.net/media/2017/11/davidnicalc.gif" alt="" width="233" height="414" class="alignnone size-full wp-image-387" /></a>

<a href="https://msdnshared.blob.core.windows.net/media/davidniCalc_2017-11-13a.msapp">Download sample app</a> (make sure to save with <strong>.msapp</strong> extension)
&nbsp;
&nbsp;
<h1>How it works</h1>
The operation of the calculator is defined by 4 context variables (to learn more about context variables, go <a href="https://docs.microsoft.com/en-us/powerapps/working-with-variables#create-a-context-variable" rel="noopener" target="_blank">here</a>).
<ul>
	<li><strong>_val</strong>: result of the latest computation</li>
	<li><strong>_operator</strong>: what mathematical operation are we currently performing. Can be "+", "-", "*", "/" representing the four basic mathematical operations. It can also be "#", representing number entry when no operations have been applied yet.</li>
	<li><strong>_operand</strong>: the number to the right of the current operation. For example, when computing 1 + 2 = ?, _operand corresponds to the number "2".</li>
	<li><strong>_decimal</strong>: current number of decimal places. Helps take care of entering numbers with decimal points</li>
</ul>
&nbsp;
<h2>The initial state</h2>

We start by setting all variables to their initial values. We do this on the screen OnVisible, and also on the Clear button's OnSelect:

<pre>UpdateContext({_val:0,_operator:"",_operand:0,_decimal:0})</pre>
&nbsp;
&nbsp;
<h2>Number entry</h2>
Each time a digit 0-9 is pressed, we update the value of <strong>_operand</strong> to include the new digit. Forgetting about decimal points for a moment, the logic for integers is quite simple: we multiply the number we already have by 10, and then add the new digit. A little care is needed when a user is entering a negative number (which they can by using the "+/-" button. In that case, we need to subtract the digit that was pressed instead of adding it.

Taking care of decimal points is slitghly more challenging, but not too much. We keep track of the number of decimal places already entered with the variable <strong>_decimal</strong>. When the "." button is pressed, we set <strong>_decimal</strong> to 1. After every key press of 0-9 after this, we update <strong>_operand</strong> by adding (or subtrating if the number is negative, as mentioned above) 10^(-_decimal)*DIGIT_PRESSED, where DIGIT_PRESSED is just the number of the button that was pressed (0 - 9).

Here is the formula for button 5's OnSelect:
<pre>
UpdateContext({
  _operand: If(_decimal&gt;0,_operand+If(_operand&lt;0,-1,1)*10^-_decimal*5, _operand*10+If(_operand&lt;0,-1,1)*5),
  _decimal: If(_decimal&gt;0, _decimal+1, 0),
  _operator: If(IsBlank(_operator), &quot;#&quot;, _operator)
})
</pre>
&nbsp;
<h2>The four mathematical operations</h2>
The logic for each of the mathematical operations can be described as a sequence of two steps:
<ol>
	<li>Update <strong>_val</strong> with the result of the previous mathematical operation</li>
	<li>Prepare for the next mathematical operation</li>
</ol>

The first part is taken care of as follows:
<pre>
UpdateContext({_val:If(_operator="#", _operand,
                       _operator="+", _val+_operand,
                       _operator="-", _val-_operand,
                       _operator="*", _val*_operand,
                       _operator="/", _val/_operand,
                       _val)})
</pre>

The way to read that is like this:
<ul>
	<li>If <strong>_operator</strong> is "#", we set <strong>_val</strong> to the current value of <strong>_operand</strong> (i.e. we store the initial number entered by the user).</li>
	<li>If _operator is "+", "-", "*", "/", we apply the corresponding operation between <strong>_val</strong> and <strong>_operand</strong>, and store the result in <strong>_val</strong>.</li>
	<li>Otherwise, we leave <strong>_val</strong> untouched. This can happen if a user pressed any of the mathematical operations or the equals sign before entering any numbers.</li>
</ul>

The second step, preparing for the next mathematical operation, is simply a matter of clearing the variables <strong>_operand</strong> and <strong>_decimal</strong>, and setting the new <strong>_operator</strong> as appropriate:
<pre>
UpdateContext({_operand:0,
               _decimal:0,
               _operator:"+"})
</pre>

The "Equals" button follows a similar logic, but sets <strong>_operator</strong> to "" (empty string, so that the next time a number is entered, it will replace <strong>_val</strong>).
&nbsp;
<h2>The Percentage button</h2>

The percentage button works by updating <strong>_operand</strong> to be a percentage of the current <strong>_val</strong>. Nice and easy:
<pre>
UpdateContext({_operand:_val*_operand/100})
</pre>
&nbsp;
<h2>Number display</h2>
Perhaps the most challenging aspect of the implementation was the logic to display the right thing as the large numbers. In some cases, we want to show the result <strong>_val</strong>. In others, we want to show the <strong>_operand</strong> as it is being entered. Thanks to the use of the pseudo-operator "#", this is what I ended up with.

The big numbers:
<pre>
<strong>lblDisplay.Text =</strong> Round(If(IsBlank(_operator) || _operand=0, _val, _operand),6)
</pre>

And the smaller text above the large numbers, that indicates the current operation:
<pre>
<strong>lblOperation.Text =</strong>
If(_operator = "#", Round(_operand,3) &amp; "",
   !IsBlank(_operator), Round(_val,3) &amp; " " &amp; _operator &amp; " " &amp; Round(_operand,3),
   Round(_val,3))
</pre>
&nbsp;
<h1>Wrapping up</h1>
That's it for the calculator. Another aspect worth mentioning is the use of dynamic layout for the buttons. I configured the buttons so that the spacings between each button and the screen borders are all the same, and the buttons resize themselves to fill up the space available.

There are many ways to achieve this. Here's what I used:
<ul>
	<li>The <strong>X</strong> position of the top-left button ("Clear") defines all the spacings. If I drag it all the way to the left, there will be zero spacing between the buttons. If I make <strong>X</strong> = 5,
 there will be 5 units of spacing between all elements.</li>
	<li>The <strong>Y</strong> position of the top-left button ("Clear") dictates the vertical positioning of all elements.</li>
</ul>

Formulas on the <strong>X</strong>, <strong>Y</strong>, <strong>Width</strong> and <strong>Height</strong> of all other buttons are used to adjust to the X and Y where I place the top-left button.

This choice allows we to very easily manipulate the calculator layout by simply moving the "Clear" button, and everything else falls into place. Comping up with the right expressions to make this work can be tricky, and drawing things out on a piece of paper first can help. An example of the freedom this provides is shown below, where I am simply moving the "Clear" button and everything else follows from it.

<a href="https://msdnshared.blob.core.windows.net/media/2017/11/davidnicalc_layout2.gif"><img src="https://msdnshared.blob.core.windows.net/media/2017/11/davidnicalc_layout2.gif" alt="" width="257" height="457" class="alignnone size-full wp-image-375" /></a>

And that's the end of this post. Let me know if you have questions or suggestions for future posts, and I'll see you next time.
]]></content:encoded>
    </item>
    <item>
      <title>Using Fiddler to simulate AAD failures with ADAL.js</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/using-fiddler-to-simulate-aad-failures-with-adal-js</link>
      <pubDate>Thu, 06 Jul 2017 21:36:37 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/?p=345</guid>
      <description><![CDATA[Authentication can be hard. Getting your error handling logic right can be especially hard if you...]]></description>
      <content:encoded><![CDATA[Authentication can be hard. Getting your error handling logic right can be especially hard if you cannot easily reproduce the error conditions that one needs to handle. This article provides some background on the Implicit Grant flow with Azure Active Directory (AAD) as supported by ADAL.js, then shows how to use <a href="https://www.telerik.com/fiddler" target="_blank">Fiddler</a> to simulate various error conditions.
<!--more-->

<h2>Scope of this article</h2>
Microsoft offers a set of libraries for dealing with identity in the cloud with AAD (Azure Active Directory), supporting various authentication flows such as <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-1.3.1" rel="noopener noreferrer">OAuth 2 Authentication Code</a> or <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-1.3.2" rel="noopener noreferrer">OAuth 2 Implicit Grant</a> flows. This article is meant exclusively for the latter (Implicit Grant flow), where a web application obtains access tokens directly from AAD without requiring intermediate servers to process any responses. In this case, we are using the <a target="_blank" href="https://github.com/AzureAD/azure-activedirectory-library-for-js/tree/master" rel="noopener noreferrer">ADAL.js</a> library.

This article is not going to talk about how to use or configure ADAL.js. There are several other sources of information about those. Instead, my focus will be on how to test your application for some of the common AAD errors you or your users might encounter to make sure that you can properly detect and react to them.
<h2>Background</h2>
The Implicit Grant flow is commonly used for authentication in modern Single Page Web Applications (SPA). <a target="_blank" href="https://teams.microsoft.com" rel="noopener noreferrer">Microsoft Teams</a> and <a target="_blank" href="https://create.powerapps.com" rel="noopener noreferrer">Microsoft PowerApps Studio for Web</a> are examples of products that use AAD Implicit Grant for authentication at the time of this writing.

Access tokens are obtained by pointing a browser window (or iframe) to an AAD endpoint and specifying details of the request as query parameters. Under the right circumstances, AAD will respond with an HTTP 302 (Redirect) status code, causing the window (or iframe) to navigate to the specified redirect_uri and the desired access token will be available in the fragment (that is, after a '#' character in the destination uri).

Example flow:
<ul>
 	<li>To obtain an access token, ADAL.js creates an iframe pointed at:
<pre>https://login.microsoftonline.com/common/oauth2/authorize?response_type=token&amp;client_id=12345678-90ab-cdef-1234-567890abcdef&amp;resource=https%3A%2F%2Fsome.resource.example.com%2F&amp;redirect_uri=https%3A%2F%2Fmysite.example.com%2Fauthframe&amp;state=fedcba98-7654-3210-fedc-ba9876543210%7Chttps%3A%2F%2Fsome.resource.example.com%2F</pre>
</li>
 	<li>On success, AAD responds as follows:
<pre>HTTP/1.1 302 Redirect
Location: https://mysite.example.com/authframe#access_token=eyJmYWtlIjp0cnVlfQ.eyJmYWtlIjp0cnVlfQ.eyJmYWtlIjp0cnVlfQ&amp;token_type=Bearer&amp;expires_in=3599&amp;state=fedcba98-7654-3210-fedc-ba9876543210%7Chttps%3A%2F%2Fsome.resource.example.com%2F</pre>
</li>
</ul>
<h2>When things go wrong</h2>
Several things can go wrong in this flow. Some causes are due to code or configuration defects. You, as the developer, are supposed to fix any such issues yourself, hopefully before your users encounter them. Other failures, however, can happen at run-time depending on user access policies and browser settings. A robust web application must be able to detect and react to these. Here are some examples of responses you should expect.
<ul>
 	<li>If the user is not signed in (e.g. cleared browser cookies), AAD might respond as follows:
<pre>HTTP/1.1 302 Redirect
Location: https://mysite.example.com/authframe#error=login_required&amp;error_description=AADSTS50058%3a+A+silent+sign-in+request+was+sent+but+no+user+is+signed+in.+The+cookies+used+to+represent+the+user%27s+session+were+not+sent+in+the+request+to+Azure+AD.+This+can+happen+if+the+user+is+using+Internet+Explorer+or+Edge%2c+and+the+web+app+sending+the+silent+sign-in+request+is+in+different+IE+security+zone+than+the+Azure+AD+endpoint+(login.microsoftonline.com).%0d%0aTrace+ID%3a+00112233-4455-6677-8899-aabbccddeeff%0d%0aCorrelation+ID%3a+ffeeddcc-bbaa-9988-7766-554433221100%0d%0aTimestamp%3a+2017-07-07+04%3a30%3a00Z&amp;state=fedcba98-7654-3210-fedc-ba9876543210%7Chttps%3A%2F%2Fsome.resource.example.com%2F</pre>
</li>
 	<li>If the desired resource requires multi-factor auth (MFA), but the user has not gone through MFA yet, AAD might respond as follows:
<pre>HTTP/1.1 302 Redirect
Location: https://mysite.example.com/authframe#error=interaction_required&amp;error_description=AADSTS50076%3A+Due+to+a+configuration+change+made+by+your+administrator%2C+or+because+you+moved+to+a+new+location%2C+you+must+use+multi-factor+authentication+to+access+'01122334-4556-6778-899a-abbccddeeff0'.%0D%0ATrace+ID%3A+00112233-4455-6677-8899-aabbccddeeff%0D%0ACorrelation+ID%3A+ffeeddcc-bbaa-9988-7766-554433221100%0D%0ATimestamp%3A+2017-07-07+04%3A30%3A00Z&amp;state=fedcba98-7654-3210-fedc-ba9876543210%7Chttps%3A%2F%2Fsome.resource.example.com%2F</pre>
</li>

 	<li>If you specify a redirect_uri that is not registered with AAD, AAD will refuse to follow it, so you will not get the usual HTTP 302 (Redirect) response. Instead, AAD will return 200 (OK) with HTML content that would show an error to the user, including a human-readable error message that indicates you have an invalid redirect_uri. In ADAL.js, errors such as this are usually identified as a timeout (i.e., ADAL.js was waiting for the page to be redirected to the redirect_uri, but this doesn't happen and after a few seconds ADAL.js gives up). Look for the constant <code>LOADFRAME_TIMEOUT</code> in the <a target="_blank" href="https://github.com/AzureAD/azure-activedirectory-library-for-js/blob/master/lib/adal.js#L86" rel="noopener noreferrer">ADAL.js source code</a> for more info.
</ul>
<h2>Simulating failures with Fiddler</h2>
If you paid attention to the examples above, you may have noticed that each of the responses includes the same <code>state</code> value as was passed on the request from the browser. It is critical to make the simulated response behave the same way. We will leverage Fiddler's FiddlerScript for this.
<ol>
	<li>Ensure you have a recent version of <a href="https://www.telerik.com/fiddler" target="_blank">Fiddler</a> installed and that it is configured to decrypt HTTPS traffic.
<blockquote>
Note: There are security implications to doing this. Make sure you are comfortable with the potential risks involved, some of which are described <a href="https://www.telerik.com/blogs/faq---certificates-in-fiddler" target="_blank">here</a>.
</blockquote></li>
	<li>On the right panel, select the FiddlerScript tab</li>
	<li>Find the <code>OnBeforeRequest</code> method definition and add the following at the end of the method body:
<pre style="color:#000020;background:#f6f8ff"><span style="color:#200080;font-weight:bold">if</span> <span style="color:#308080">(</span><span style="color:#008c00">1</span> <span style="color:#308080">&amp;&amp;</span>
    oSession<span style="color:#308080">.</span>oRequest<span style="color:#308080">.</span>host <span style="color:#308080">==</span> <span style="color:#800000">"</span><span style="color:#1060b6">login.microsoftonline.com</span><span style="color:#800000">"</span> <span style="color:#308080">&amp;&amp;</span>
    oSession<span style="color:#308080">.</span>HTTPMethodIs<span style="color:#308080">(</span><span style="color:#800000">"</span><span style="color:#1060b6">GET</span><span style="color:#800000">"</span><span style="color:#308080">)</span><span style="color:#308080">)</span> <span style="color:#406080">{</span>
    <span style="color:#200080;font-weight:bold">var</span> url <span style="color:#308080">=</span> oSession<span style="color:#308080">.</span>fullUrl<span style="color:#406080">;</span>
    <span style="color:#200080;font-weight:bold">var</span> end <span style="color:#308080">=</span> url<span style="color:#308080">.</span>Length<span style="color:#406080">;</span>
    <span style="color:#200080;font-weight:bold">var</span> qmark <span style="color:#308080">=</span> url<span style="color:#308080">.</span>IndexOf<span style="color:#308080">(</span><span style="color:#800000">"</span><span style="color:#1060b6">?</span><span style="color:#800000">"</span><span style="color:#308080">)</span><span style="color:#406080">;</span>
    <span style="color:#200080;font-weight:bold">var</span> qparams <span style="color:#308080">=</span> <span style="color:#406080">{</span><span style="color:#406080">}</span><span style="color:#406080">;</span>
    <span style="color:#200080;font-weight:bold">if</span> <span style="color:#308080">(</span>qmark <span style="color:#308080">&gt;=</span> <span style="color:#008c00">0</span><span style="color:#308080">)</span> <span style="color:#406080">{</span>
        <span style="color:#200080;font-weight:bold">var</span> query <span style="color:#308080">=</span> url<span style="color:#308080">.</span>Substring<span style="color:#308080">(</span>qmark <span style="color:#308080">+</span> <span style="color:#008c00">1</span><span style="color:#308080">,</span> end <span style="color:#308080">-</span> qmark <span style="color:#308080">-</span> <span style="color:#008c00">2</span><span style="color:#308080">)</span><span style="color:#406080">;</span>
        qparams <span style="color:#308080">=</span> Utilities<span style="color:#308080">.</span>ParseQueryString<span style="color:#308080">(</span>query<span style="color:#308080">)</span><span style="color:#406080">;</span>
    <span style="color:#406080">}</span>
    <span style="color:#200080;font-weight:bold">if</span> <span style="color:#308080">(</span>qparams<span style="color:#308080">[</span><span style="color:#800000">"</span><span style="color:#1060b6">response_type</span><span style="color:#800000">"</span><span style="color:#308080">]</span> <span style="color:#308080">===</span> <span style="color:#800000">"</span><span style="color:#1060b6">token</span><span style="color:#800000">"</span><span style="color:#308080">)</span> <span style="color:#406080">{</span>
        oSession<span style="color:#308080">[</span><span style="color:#800000">"</span><span style="color:#1060b6">ui-color</span><span style="color:#800000">"</span><span style="color:#308080">]</span> <span style="color:#308080">=</span> <span style="color:#800000">"</span><span style="color:#1060b6">purple</span><span style="color:#800000">"</span><span style="color:#406080">;</span> <span style="color:#595979">// Visually indicate this request was modified</span>
        oSession<span style="color:#308080">[</span><span style="color:#800000">"</span><span style="color:#1060b6">ui-backcolor</span><span style="color:#800000">"</span><span style="color:#308080">]</span> <span style="color:#308080">=</span> <span style="color:#800000">"</span><span style="color:#1060b6">cyan</span><span style="color:#800000">"</span><span style="color:#406080">;</span>
        oSession<span style="color:#308080">.</span>utilCreateResponseAndBypassServer<span style="color:#308080">(</span><span style="color:#308080">)</span><span style="color:#406080">;</span>
        <span style="color:#200080;font-weight:bold">var</span> newLocation <span style="color:#308080">=</span> qparams<span style="color:#308080">[</span><span style="color:#800000">"</span><span style="color:#1060b6">redirect_uri</span><span style="color:#800000">"</span><span style="color:#308080">]</span> <span style="color:#308080">+</span> <span style="color:#800000">"</span><span style="color:#1060b6">#error=login_required&amp;error_description=Simulated+aad+error&amp;state=</span><span style="color:#800000">"</span> <span style="color:#308080">+</span> Utilities<span style="color:#308080">.</span>UrlEncode<span style="color:#308080">(</span>qparams<span style="color:#308080">[</span><span style="color:#800000">"</span><span style="color:#1060b6">state</span><span style="color:#800000">"</span><span style="color:#308080">]</span><span style="color:#308080">)</span><span style="color:#406080">;</span>
        oSession<span style="color:#308080">.</span>responseCode <span style="color:#308080">=</span> <span style="color:#008c00">302</span><span style="color:#406080">;</span>
        oSession<span style="color:#308080">.</span>oResponse<span style="color:#308080">.</span>headers<span style="color:#308080">.</span>Add<span style="color:#308080">(</span><span style="color:#800000">"</span><span style="color:#1060b6">Location</span><span style="color:#800000">"</span><span style="color:#308080">,</span> newLocation<span style="color:#308080">)</span><span style="color:#406080">;</span>
    <span style="color:#406080">}</span>
<span style="color:#406080">}</span>
</pre>
</li>
<li>To easily enable or disable this code, simply change <code>if (1 &amp;&amp;</code> on the first line to <code>if (0 &amp;&amp;</code>.</li>
<li>When Fiddler is capturing, any web applications that attempts to acquire tokens using AAD's Implicit Grant Flow will now experience the simulated error. You can easily add additional checks to only intercept requests meant for your AAD app client_id. The variable <code>qparams</code> above is your friend.</li>
</ol>

<h2>Wrapping up</h2>
That's it for now. I hope this helps some people out there test their applications and ensure they can handle these kinds of errors gracefully. Let me know if you have comments or suggestions and I will try to address them. See you next time!]]></content:encoded>
    </item>
    <item>
      <title>Scatter plots in PowerApps using a Gallery control</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/scatter-plots-in-powerapps-using-a-gallery-control</link>
      <pubDate>Thu, 25 May 2017 15:05:35 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/?p=315</guid>
      <description><![CDATA[This article will show you how you can build custom data visualizations in PowerApps using only two...]]></description>
      <content:encoded><![CDATA[This article will show you how you can build custom data visualizations in PowerApps using only two controls: a Gallery, and a Circle.

While PowerApps already supports Column charts, Line charts and Pie charts out of the box, there is currently no built-in way to render scatter plots. Note that the idea presented here is only useful if you have small amounts of data. This is not a replacement for more advanced data visualization tools such as the ones that exist with Power BI.

<a href="https://msdnshared.blob.core.windows.net/media/2017/05/scatter_screenshot.png"><img src="https://msdnshared.blob.core.windows.net/media/2017/05/scatter_screenshot-202x300.png" alt="" width="202" height="300" class="alignnone wp-image-325 size-medium" /></a>

<a href="https://msdnshared.blob.core.windows.net/media/davidniScatterPlot2.msapp">Download sample app</a>

&nbsp;

<h2>How it works</h2>
The trick is to use a vertical Gallery with a very small TemplateSize. The gallery template will have a single Circle control, whose <strong>X</strong>, <strong>Y</strong>, <strong>Width</strong>, <strong>Height</strong>, and <strong>Fill</strong> will be bound to fields of a data source. The Gallery works by replicating its template for as many items as exists in the data source it is bound to, so if we have some data with a few entries, we will get one circle for each entry. In addition, PowerApps ensures gallery items that extend beyond the template size are not clipped and will render correctly even if TempalteSize is much smaller than the circles. So, are we all set?

Well, almost. If we could set the TemplateSize to 0, we would be done. However, the minimum value allowed in PowerApps is TemplateSize=1. This means that each replicated circle will be one pixel below the one before it, and the circles would not render at exactly the right place. We can easily fix that by having each item in the gallery have a field with the item index in the gallery, and offset the circle's Y position accordingly to compensate for the gallery TemplateSize.

&nbsp;

<h2>Formula time</h2>
Here's how we add items to the data source (it is just a collection in this case). Note that we are using context variables <strong>minRadius</strong> and <strong>maxRadius</strong> just to make it easier to change those values in the sample. You could hard code them or get them from somewhere else instead just as easily.
<pre>UpdateContext({minRadius:10, maxRadius:30});
Collect(Bubbles,
  {
    index: CountRows(Bubbles),
    x: Rand() * (Gallery1.Width - maxRadius*2) + maxRadius,
    y: Rand() * (Gallery1.Height - maxRadius*2) + maxRadius,
    radius: Rand() * (maxRadius-minRadius) + minRadius,
    color: RGBA(Rand() * 255, Rand() * 255, 0, 1)
  })
</pre>
And here's how the circle in the gallery template is configured.
<pre><strong>Circle1.X</strong>      = ThisItem.x-ThisItem.radius
<strong>Circle1.Y</strong>      = ThisItem.y-ThisItem.radius-ThisItem.index*Gallery1.TemplateHeight
<strong>Circle1.Width</strong>  = ThisItem.radius*2
<strong>Circle1.Height</strong> = ThisItem.radius*2
<strong>Circle1.Fill</strong>   = ThisItem.color
</pre>

&nbsp;

<h2>Wrapping up</h2>
And that's it for now. Now you know how to leverage a Gallery control to do more than just show rows of data. The possibilities are endless, but remember that this is not exactly the scenario that Galleries were designed and optimized for, so always try to use the built-in data visualization controls when possible and send us feedback of what you're trying to build and what would make things easier for you.

See you next time!
<blockquote>PS: This article was inspired by a <a target="_blank" href="https://powerusers.microsoft.com/t5/PowerApps-Forum/Add-content-icons-dynamically-in-PowerApp/m-p/37261">question in the PowerApps forums</a>. Thanks <em>JohnP</em> for the questions and keep them coming!</blockquote>]]></content:encoded>
    </item>
    <item>
      <title>Text conversion power tool for Visual Studio Code - Inception</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/text-conversion-power-tool-for-visual-studio-code-inception</link>
      <pubDate>Tue, 21 Feb 2017 23:50:34 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/?p=285</guid>
      <description><![CDATA[Last week I finally wrote a tool that I had been meaning to write for some time. Inception is an...]]></description>
      <content:encoded><![CDATA[Last week I finally wrote a tool that I had been meaning to write for some time. <strong>Inception</strong> is an extension for Visual Studio Code that helps developers extract information contained in text with simple keystrokes and few clicks.
&nbsp;
Get it here: <a href="https://marketplace.visualstudio.com/items?itemName=davidni.inception">https://marketplace.visualstudio.com/items?itemName=davidni.inception</a>
Source code: <a href="https://github.com/davidni/vscode-inception">https://github.com/davidni/vscode-inception</a>
&nbsp;
<a href="https://msdnshared.blob.core.windows.net/media/2017/02/Inception_demo2.gif"><img src="https://msdnshared.blob.core.windows.net/media/2017/02/Inception_demo2.gif" alt="inception_demo" width="1024" height="544" class="alignnone size-large wp-image-295" /></a>
&nbsp;
<h2>What does it do?</h2>
<strong>Inception</strong> contributes a new command to VS Code which examines selected text and tries to guess what it is, then provides useful outputs for common things you may want to do with it.

For example:
<ul>
 	<li>Select a base64-encoded string, press Ctrl+F1 (Cmd+F1 on Mac) and see the decoded data</li>
 	<li>Select a URL-encoded string, press Ctrl+F1 (Cmd+F1 on Mac) and see the decoded data</li>
 	<li>Select a URL, press Ctrl+F1 (Cmd+F1 on Mac) and see its query and hash parameters nicely decoded</li>
 	<li>Select a JSON string, press Ctrl+F1 (Cmd+F1 on Mac) and see the prettified data</li>
 	<li>Select a UNIX timestamp, press Ctrl+F1 (Cmd+F1 on Mac) and see the decoded date/time in your time-zone as well as in UTC.</li>
 	<li>Select a JWT token, press Ctrl+F1 (Cmd+F1 on Mac) and see its base64 parts nicely separated and identified.</li>
 	<li>etc.</li>
</ul>
&nbsp;
No more copying / pasting text around into separate converters and tools. No more learning how to use different tools for simple conversion operations. No more risking using online tools that could be stealing your data.

This has proven to be useful for my own workflows and perhaps others can benefit from it or contribute support for more encodings. It is fully open source and contributions are very welcome.]]></content:encoded>
    </item>
    <item>
      <title>Advanced PowerApps visualizations with dynamic SVG’s</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/advanced-powerapps-visualizations-with-dynamic-svgs</link>
      <pubDate>Sun, 08 Jan 2017 03:57:45 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/?p=235</guid>
      <description><![CDATA[PowerApps brings you lots of built-in controls and ways to visualize your data with just a few...]]></description>
      <content:encoded><![CDATA[PowerApps brings you lots of built-in controls and ways to visualize your data with just a few clicks. Sometimes, however, you might want to build custom visualizations that are not quite achievable with only those controls. This article shows one way in which you can create your own custom visualizations. In particular, we will create a radial gauge (circular progress bar) using only an Image control. This is not for the faint of heart, however. Proceed at your own risk :)

<a href="https://msdnshared.blob.core.windows.net/media/2017/01/RadialGaugeAnim.gif"><img src="https://msdnshared.blob.core.windows.net/media/2017/01/RadialGaugeAnim.gif" alt="radialgaugeanim" width="245" height="280" class="alignnone size-full wp-image-255" /></a>

<ul>
  <li><a href="https://patemplates.azurewebsites.net/templates/f84d933e-ef88-4890-91fa-a9b472cefe96" rel="noopener" target="_blank">Get the template</a> (open the link, press Ctrl+C, then paste onto your app)</li>
  <li><a href="https://msdnshared.blob.core.windows.net/media/davidniGaugeSample.msapp">Download sample app</a></li>
</ul>

<blockquote>NOTE: The technique explained in this article is not officially supported by the product, nor is it easy or friendly to use. You should prefer other supported alternatives when possible. That said, if nothing else suits your needs and this allows you to build the amazing PowerApp you want, then by all means go for it and have fun! I have tested it to ensure that it currently works in all our supported platforms as of this writing.</blockquote>
<h2>Some background</h2>
We use 2 key concepts to achieve our goals in this article:
<ul>
 	<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs">Data-uris</a>: this is a standardized way of expressing small data items in the form of a URL. The <a target="_blank" href="https://powerapps.microsoft.com/en-us/tutorials/control-image/">PowerApps Image control</a> accepts URL’s to specify an image, and we will use this special type of URL to define an image inline.</li>
 	<li><a target="_blank" href="https://en.wikipedia.org/wiki/Scalable_Vector_Graphics">SVG's</a>: Scalable Vector Graphics are becoming the de-facto way to represent vector graphics. It is very convenient in our case because SVG is an XML-based format, so it can be expressed in plain-text.</li>
</ul>
The idea: Write a PowerApps expression that results in some text which will be the data-uri representation of an SVG image. By setting this expression as the <strong>Image</strong> property of an Image control, we can dynamically render the SVG content, which can take any valid PowerApps constructs as input. The sample app shows a radial gauge representation that is controlled by a Slider control. Of course, you can create any other visualization with the same approach.
<h2>The magic expression</h2>
<pre>"data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100px%22%20height%3D%22100px%22%3E" &amp;
    "%3Cpath%20fill%3D%22none%22%20style%3D%22<strong><span style="color: #ff00ff">stroke-width%3A18</span></strong>%3B<strong><span style="color: #ff0000">stroke%3Argb%28230%2C230%2C230%29</span></strong>%3Bstroke-linecap%3Around%3B%22%20d%3D%22" &amp;
        "M%20" &amp;
        Text(50-40*Sin(<strong><span style="color: #339966">Pi()/4</span></strong>)) &amp; "%20" &amp; Text(50+40*Cos(<strong><span style="color: #339966">Pi()/4</span></strong>)) &amp;
        "%20A%20" &amp;
        "40%2040%200%201%201%20" &amp; Text(50-40*Sin(2*Pi()-<strong><span style="color: #339966">Pi()/4</span></strong>)) &amp; "%20" &amp; Text(50+40*Cos(2*Pi()-<strong><span style="color: #339966">Pi()/4</span></strong>)) &amp;
        "%22%3E" &amp;
    "%3C%2Fpath%3E" &amp;
    "%3Cpath%20fill%3D%22none%22%20style%3D%22<strong><span style="color: #ff00ff">stroke-width%3A18</span></strong>%3B<strong><span style="color: #0000ff">stroke%3Argb%2869%2C204%2C206%29</span></strong>%3Bstroke-linecap%3Around%3B%22%20d%3D%22" &amp;
        "M%20" &amp;
        Text(50-40*Sin(<strong><span style="color: #339966">Pi()/4</span></strong>)) &amp; "%20" &amp; Text(50+40*Cos(<strong><span style="color: #339966">Pi()/4</span></strong>)) &amp;
        "%20A%20" &amp;
        "40%2040%200%20" &amp; If(<strong><span style="color: #ff6600">Slider1.Value/Slider1.Max</span></strong>&gt;Pi()/(2*(Pi()-<strong><span style="color: #339966">Pi()/4</span></strong>)),"1","0") &amp; "%201%20" &amp; Text(50-40*Sin(<strong><span style="color: #339966">Pi()/4</span></strong>+2*(Pi()-<strong><span style="color: #339966">Pi()/4</span></strong>)*<strong><span style="color: #ff6600">Slider1.Value/Slider1.Max</span></strong>)) &amp; "%20" &amp; Text(50+40*Cos(<strong><span style="color: #339966">Pi()/4</span></strong>+2*(Pi()-<strong><span style="color: #339966">Pi()/4</span></strong>)*<strong><span style="color: #ff6600">Slider1.Value/Slider1.Max</span></strong>)) &amp;
        "%22%3E" &amp;
    "%3C%2Fpath%3E" &amp;
"%3C%2Fsvg%3E"
</pre>
You can change the look and feel by looking for the following chunks in the given expression and changing the values of all occurrences to something else. If you change any one of these, make sure you find and replace all occurrences, otherwise things may not work.
<ul>
 	<li><span style="color: #ff0000">stroke%3Argb%28230%2C230%2C230%29</span>: URL-encoded value of the light-gray radial background color. In this example, the R, G and B components all have value 230</li>
 	<li><span style="color: #0000ff">stroke%3Argb%2869%2C204%2C206%29</span>: URL-encoded value of the blue radial indicator color. In this example, R=69, G=204, B=206.</li>
 	<li><span style="color: #ff00ff">stroke-width%3A18</span>: Controls the thickness of the indicator background and foreground. For this example they are both the same (18), but you can change them independently if desired.</li>
 	<li><span style="color: #ff6600">Slider1.Value/Slider1.Max</span>: The PowerApps expression giving a value from 0 to 1 for the value you want to show in the visualization. In this case we use the position of <strong>Slider1</strong> to control it.</li>
 	<li><span style="color: #339966">Pi()/4</span>: controls where the radial indicator starts at the bottom in radians. As you increase this, the spacing at the bottom increases too (see image below -- make sure this is never greater than Pi [180 degrees]).
<a href="https://msdnshared.blob.core.windows.net/media/2017/01/RadialGaugeMath.png"><img src="https://msdnshared.blob.core.windows.net/media/2017/01/RadialGaugeMath.png" alt="radialgaugemath" width="166" height="160" class="alignnone size-full wp-image-265" /></a></li>
</ul>
&nbsp;
<h2>A note on image resizing</h2>
Since we are using SVG, the image can be rendered at any resolution without looking blurry. You can resize the Image control to any size you want (just make sure the property ‘<strong>ImagePosition</strong>’ is set to ‘<strong>Fit</strong>’) and it will scale as appropriate.
<h2>Troubleshooting</h2>
A single character error could prevent this from working, in which case you would get a blank image and no indication of what might be wrong. Should that happen, a helpful tip is to add a TextBox to your app and set its Text property to <strong>Image1.Image</strong>. This will show you the result of evaluating your expression, and might help you find what’s wrong. For the sample app and when the slider is exactly in the center, this is what it should look like:
<blockquote>NOTE: If you are using Chrome, you can copy the string below and paste it in the address bar of your browser and you will see the radial gauge come up. Browsers too can read data uri's!</blockquote>
<pre>data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100px%22%20height%3D%22100px%22%3E%3Cpath%20fill%3D%22none%22%20style%3D%22stroke-width%3A18%3Bstroke%3Argb%28230%2C230%2C230%29%3Bstroke-linecap%3Around%3B%22%20d%3D%22M%2021.71572875%2078.28427125%20A%2040%2040%200%201%201%2078.28427125%2078.28427125%22%3E%3C%2Fpath%3E%3Cpath%20fill%3D%22none%22%20style%3D%22stroke-width%3A18%3Bstroke%3Argb%2869%2C204%2C206%29%3Bstroke-linecap%3Around%3B%22%20d%3D%22M%2021.71572875%2078.28427125%20A%2040%2040%200%200%201%2050.0%2010%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E
</pre>
When URL-decoded and formatted, you can see what the SVG content looks like:
<pre>&lt;svg xmlns="https://www.w3.org/2000/svg" width="100px" height="100px"&gt;
  &lt;path fill="none"
        style="stroke-width:18;stroke:rgb(230,230,230);stroke-linecap:round;"
        d="M 21.71572875 78.28427125 A 40 40 0 1 1 78.28427125 78.28427125"&gt;&lt;/path&gt;
  &lt;path fill="none"
        style="stroke-width:18;stroke:rgb(69,204,206);stroke-linecap:round;"
        d="M 21.71572875 78.28427125 A 40 40 0 0 1 50.0 10"&gt;&lt;/path&gt;
&lt;/svg&gt;
</pre>
&nbsp;
<h2>Wrapping up</h2>
And that's it for now. I mentioned in the beginning that this was not for the faint of heart, and now you can probably see why.

In any case, this opens up many interesting scenarios if you are willing to have some not-very-pretty expressions in your app. What's more, whoever is using your app (perhaps yourself!) won't know about the expressions that are powering the visualization. All they will see is the beautiful UI that you were able to create thanks to PowerApps.

See you next time!
<blockquote>PS: This article was inspired by a <a target="_blank" href="https://powerusers.microsoft.com/t5/PowerApps-Forum/Circular-progress-bar-in-power-apps/td-p/17066">question in the PowerApps forums</a>. Thanks <em>nagavital</em> for the questions and keep them coming!</blockquote>]]></content:encoded>
    </item>
    <item>
      <title>Building an interactive calendar view with PowerApps</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/building-an-interactive-calendar-view-with-powerapps</link>
      <pubDate>Tue, 13 Dec 2016 02:46:20 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/?p=146</guid>
      <description><![CDATA[PowerApps has many built-in facilities to help you work with dates and calendars (click these links...]]></description>
      <content:encoded><![CDATA[PowerApps has many built-in facilities to help you work with dates and calendars (click these links to learn more about the <a href="https://powerapps.microsoft.com/en-us/tutorials/control-date-picker/">DatePicker control</a> and the Date and DateTime functions <a href="https://powerapps.microsoft.com/en-us/tutorials/function-datevalue-timevalue/">1</a>, <a href="https://powerapps.microsoft.com/en-us/tutorials/function-date-time/">2</a>, <a href="https://powerapps.microsoft.com/en-us/tutorials/function-dateadd-datediff/">3</a>).

For some scenarios, however, you might want to customize the look and feel of those experiences beyond what is available out of the box. This article will walk you through the creation of an app with a customized calendar that shows a month view, highlights special days (holidays and weekends) and allows for interactive date range selection by tapping. Here is what it will look like:

<a href="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image1.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image1.png" alt="cal-image1" width="280" height="334" class="wp-image-155 size-full alignnone" /></a>

&nbsp;

<ul>
  <li><a href="https://patemplates.azurewebsites.net/templates/4b77bfd8-811e-4a41-88d3-c129b96f2d2d" rel="noopener" target="_blank">Get the template</a> (open the link, press Ctrl+C, then paste onto your app)</li>
  <li><a href="https://msdnshared.blob.core.windows.net/media/davidniCalendarDemo_2016-12-12-18-30.msapp">Download the sample app</a> (you will need PowerApps - get started at <a href="https://powerapps.microsoft.com">https://powerapps.microsoft.com</a>)</li>
</ul>

Since this example uses only built-in PowerApps controls such as TextBoxes, Circles, Rectangles and Galleries, you can take this further and customize it in endless ways for your own needs. Even if you have no interest in building a calendar control, this article can still help you learn a little bit more about PowerApps:
<ul>
 	<li>Using Galleries and the WrapCount property</li>
 	<li>How to find the control you want when your app has several on top of each other</li>
 	<li>Using context variables</li>
 	<li>Tricks for improving your app’s performance</li>
</ul>
&nbsp;
<h2>Introduction and strategy</h2>
Let’s outline the plan before we start executing it.

The main piece of this calendar control will be a Gallery, where each gallery item is a day in the month. The gallery template will need a TextBox to show the day number (1 – 31), and a Circle behind the text to show if that day is special (holidays will show a purple circle; selected days will show a blue circle). We will add other controls as we go.

Some calendar math: Note that we need up to 6 weeks to fit all possible months. The extreme case is a 31-day month that starts on the last day of the week (look at July 2017 for example). For our purposes, we will always show 6 weeks in our view, even if a month could fit in fewer (5 or 4) weeks. This is done for multiple reasons:
<ul>
 	<li>Our calendar will always occupy the same screen space regardless of the selected month. You won’t need to worry about adapting the rest of your app UI for months that take up more or less weeks</li>
 	<li>It is consistent with the Windows 10 Clock / calendar control (click the clock on your system tray and navigate between July/2017 to August/2017 to see this in action)</li>
 	<li>It is easier to build</li>
</ul>
Win-win-win (that said, you can change this behavior if you want since, again, we are using only PowerApps primitive controls).

We also need the Left / Right arrows to switch months, and they must do <em>something</em> that will affect the gallery. We will use <a href="https://powerapps.microsoft.com/en-us/tutorials/working-with-variables/">Context Variables</a> for this.

Finally, we want to be able to select dates by tapping on them. Remember that all controls in PowerApps have an OnSelect action that is executed when the control is tapped, and we will use the OnSelect action of the TextBox to update some Context Variables in this case.

&nbsp;
<h2>Before we begin – A trick for selecting controls</h2>
To select a specific control, a very convenient way is to use the “Advanced” pane on the right-hand side:

<a href="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image2.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image2-300x167.png" alt="cal-image2" width="300" height="167" class="alignnone size-medium wp-image-165" /></a>
<ul>
 	<li>To select a top-level control, select it from the drop-down indicated above</li>
 	<li>To select a control that is inside a gallery, first select the gallery, then click the drop-down again and you will see all controls that exist within the gallery listed below a divider line. At the end of this exercise, you will have something like this:</li>
</ul>
<a href="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image3.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image3-195x300.png" alt="cal-image3" width="195" height="300" class="alignnone size-medium wp-image-175" /></a>

&nbsp;
<h2>Initialization</h2>
Select your screen, click on "Action" on the ribbon --&gt; "On visible". Paste the expression below in the formula bar. This is setting up some context variables that we will use next.

<em>[LOCALE NOTICE] Depending on the regional settings of your device, you may have to adjust the formulas provided on this blog post slightly. You may have to replace all semicolons (</em><span>;</span><em>) with double semicolons (</em><span>;;</span><em>), and all commas (</em><span>,</span><em>) with semicolons (</em><span>;</span><em>).</em> Learn more about <a href="https://powerapps.microsoft.com/en-us/tutorials/global-apps/">Languages and globalization in PowerApps</a>.
<pre><strong>Screen1.OnVisible =</strong>
If(!_initialized,
  UpdateContext({_today:Today()});
  UpdateContext({_firstDayOfMonth:DateAdd(_today,1-Day(_today),Days)});
  UpdateContext({_firstDayInView:DateAdd(_firstDayOfMonth,-(Mod(Weekday(_firstDayOfMonth)-2,7)+1),Days)});
  UpdateContext({_holidays:Table({HolidayDate:Date(2016,12,25)},
                                 {HolidayDate:Date(2017,1,1)},
                                 {HolidayDate:Date(2017,1,16)},
                                 {HolidayDate:Date(2017,2,20)},
                                 {HolidayDate:Date(2017,5,29)},
                                 {HolidayDate:Date(2017,7,4)},
                                 {HolidayDate:Date(2017,9,4)},
                                 {HolidayDate:Date(2017,11,23)},
                                 {HolidayDate:Date(2017,11,24)},
                                 {HolidayDate:Date(2017,12,25)}
                                )
                });
  UpdateContext({_initialized:true})
)
</pre>
Note: I am using names with a leading underscore like _<span>this</span> for context variables to distinguish them from other entities in PowerApps such as control names and collections. You don’t have to do this, but it helps keep things organized.
<ul>
 	<li><span>_firstDayOfMonth</span> will have a Date corresponding to the first day of the current month. We do this by taking today’s date, and subtracting (current day - 1) or, equivalently, adding –(current day – 1) = (1-current day). For example, if today were February 7<sup>th</sup>, we would do (February 7<sup>th</sup>) + (1 - 7) = (February 7<sup>th</sup>) – 6 = February 1<sup>st</sup>.</li>
 	<li><span>_firstDayInView</span> is a little bit trickier. It represents the first day that shows up at the top left of the gallery, which is the first Sunday before <span>_firstDayOfMonth</span>. We leverage the function <span>Weekday</span> to do this, which returns a number from 1 (Sunday) to 7 (Saturday) for the given date. We then do some math and use the <span>Mod</span> function, which gives the remainder of a number by a divisor (7 in this case because a week has 7 days).</li>
 	<li><span>_holidays</span> is a list of records, where each record has a single field called HolidayDate and whose value is the date of a holiday. You could also pull this data from an external connection using <a href="https://powerapps.microsoft.com/en-us/blog/building-a-custom-api-for-powerapps-using-azure-app-service-web-apps/">Custom API’s</a>, or embed an Excel data source to achieve the same. Set the holidays that are meaningful to your business and / or your country by modifying this list.</li>
 	<li><span>_initialized</span> is a variable that indicates whether initialization was already done or not. This helps the user experience if your app involves navigating between Screens. When your user comes back to this screen, they will see things as they left them thanks to this context variable. If you prefer to reset everything each time a user navigates to your screen, you can remove this variable and the <span>If</span> check at the top.</li>
</ul>
&nbsp;
<h2>Making the initialization formula execute</h2>
Because you typed the OnVisible expression just now, it is ready to be executed when you navigate to this screen, but you need to do that at least once or it won’t have run. You only need to do this now because your OnVisible expression wasn’t there when PowerApps first showed your screen. From now on, if you save your app and open it again (or share with your colleagues), the OnVisible formula will execute as soon as your app starts, so you won’t have to do this again.

To force this to run now, do this:
<ul>
 	<li>Add another screen to your app (click "Insert" on the ribbon --&gt; "New screen").</li>
 	<li>Navigate back to the first screen by clicking on it on the left side.</li>
 	<li>Delete the new screen you just added. It has now served its purpose.</li>
</ul>
&nbsp;
<h2>The main gallery</h2>
<ul>
 	<li>Back in your first Screen, add a Vertical Gallery and rename it to <strong>CalendarGallery</strong>:</li>
</ul>
<pre><strong>CalendarGallery.TemplateSize =</strong> 50
<strong>CalendarGallery.Items =</strong>
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
 10,11,12,13,14,15,16,17,18,19,
 20,21,22,23,24,25,26,27,28,29,
 30,31,32,33,34,35,36,37,38,39,
 40,41]
<strong>CalendarGallery.WrapCount =</strong> 7
</pre>
<ul>
 	<li>Add a TextBox inside <strong>CalendarGallery</strong> (make sure to select the gallery template first, then add the TextBox). Rename it to <strong>TextDay</strong>.</li>
</ul>
<pre><strong>TextDay.Text =</strong> Day(DateAdd(_firstDayInView,ThisItem.Value,Days))</pre>
At this point you should be seeing something like this:

<a href="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image4.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image4.png" alt="cal-image4" width="291" height="216" class="alignnone size-full wp-image-185" /></a>

&nbsp;
<h2>Switching months</h2>
<ul>
 	<li>Add Back and Next icons outside the gallery (click "Insert" in the ribbon --&gt; "Icon") and place them above the gallery. Name them <strong>BtnBack</strong> and <strong>BtnNext</strong></li>
</ul>
<pre><strong>BtnBack.OnSelect =</strong>
UpdateContext({_firstDayOfMonth:DateAdd(_firstDayOfMonth,-1,Months)});
UpdateContext({_firstDayInView:DateAdd(_firstDayOfMonth,-(Mod(Weekday(_firstDayOfMonth)-2,7)+1),Days)})

<strong>BtnNext.OnSelect =</strong>
UpdateContext({_firstDayOfMonth:DateAdd(_firstDayOfMonth,1,Months)});
UpdateContext({_firstDayInView:DateAdd(_firstDayOfMonth,-(Mod(Weekday(_firstDayOfMonth)-2,7)+1),Days)})
</pre>
Notice that the second line of each formula is the same as what we did during OnVisible Given the first day of the newly selected month, this will calculate what is the first Sunday before it.

&nbsp;
<h2>Showing the current month</h2>
<ul>
 	<li>Add a TextBox, rename it to <strong>CurrentMonthName</strong> and place it between the back / next buttons that you added above.</li>
</ul>
<pre><strong>CurrentMonthName.Text =</strong> Text(_firstDayOfMonth,"mmmm yyyy")
</pre>
&nbsp;
<h2>Showing the weekday header (Sunday, Monday, Tuesday, …)</h2>
<ul>
 	<li>Add a Horizontal Gallery , make it about 60 pixels tall and as wide as your <strong>CalendarGallery</strong>. Name it <strong>HeadersGallery</strong> and place it just above <strong>CalendarGallery</strong>.</li>
</ul>
<pre><strong>HeadersGallery.Items           =</strong> ["S","M","T","W","T","F","S"]
<strong>HeadersGallery.TemplatePadding =</strong> CalendarGallery.TemplatePadding
<strong>HeadersGallery.TemplateSize    =</strong> CalendarGallery.TemplateWidth
<strong>HeadersGallery.Fill            =</strong> RGBA(0, 0, 0, 0.1)
</pre>
<ul>
 	<li>Add a TextBox inside the new gallery and name it <strong>WeekDayHeader</strong>.</li>
</ul>
<pre><strong>WeekDayHeader.Text       =</strong> ThisItem.Value
<strong>WeekDayHeader.Align      =</strong> Center
<strong>WeekDayHeader.FontWeight =</strong> Bold
</pre>
At this point you should have something like this:

<a href="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image5.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image5.png" alt="cal-image5" width="279" height="263" class="alignnone size-full wp-image-195" /></a>

&nbsp;
<h2>Showing holidays</h2>
<ul>
 	<li>Select the <strong>CalendarGallery</strong> template by clicking on the first item of this gallery</li>
 	<li>Add a circle ("Insert" in the ribbon --&gt; "Icons" --&gt; "Circle") and rename it to <strong>HolidayCircle</strong></li>
</ul>
<pre>
<strong>HolidayCircle.Visible =</strong> DateAdd(_firstDayInView,ThisItem.Value,Days) in _holidays

<strong>HolidayCircle.Fill =</strong>
ColorFade(
    RGBA(192,0,160,1),
    If( Month(DateAdd(_firstDayInView,ThisItem.Value,Days))=Month(_firstDayOfMonth),
        0,
        80%)
)
</pre>
The Fill formula deserves some further consideration. This is adjusting the color of the circle depending on whether the day is part of the current month or not (<span>ColorFade</span> is a function that transforms a color. It makes things lighter if you pass a positive value and darker if you pass a negative value: 100% would make it white, and -100% would make it black. We are using 80%, which will make the purple lighter but still visible).

Here is what you should see now if you go to January 2017. Notice how December 25<sup>th</sup> 2016 has a light purple circle, because it is a holiday but not in the current month. Also, note that the dark text is hard to read on top of the purple circle. We will fix that next.

<a href="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image6.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image6.png" alt="cal-image6" width="277" height="263" class="alignnone size-full wp-image-205" /></a>

&nbsp;
<h2>Adjusting the text color for better contrast</h2>
<ul>
 	<li>Select the TextBox <strong>TextDay</strong> by clicking on the first item of the main gallery. Then set this formula:</li>
</ul>
<pre><strong>TextDay.Color =</strong>
ColorFade(
    If( HolidayCircle.Visible,
        RGBA(255,255,255,1),
        RGBA(47,41,43,1)),
    If( Month(DateAdd(_firstDayInView,ThisItem.Value,Days))=Month(_firstDayOfMonth),
        0,
        80%)
)
</pre>
This is similar to what we did for the <strong>HolidayCircle</strong> fill, except that now we are controlling 2 things at the same time:
<ol type="a">
 	<li>The color is white if the day is a holiday and dark otherwise</li>
 	<li>We make it 80% lighter if the day is not in the currently selected month. This is how we make days that are not in the current month appear dimmed, as shown below:</li>
</ol>
<a href="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image7.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image7.png" alt="cal-image7" width="277" height="266" class="alignnone size-full wp-image-156" /></a>

Hopefully you would agree that this is starting to look much better.

&nbsp;
<h2>Allowing date range selection by tapping - Strategy</h2>
Here is the behavior we want to build:
<ol type="a">
 	<li>Clicking on a date selects it</li>
 	<li>Clicking on a date after having selected another date, selects the range between those 2 dates</li>
 	<li>Clicking an already-selected day clears the selection</li>
</ol>
This is an intuitive model that your users will be able to quickly understand by clicking around and exploring your app. Here is the strategy that we will use to build this:
<ol type="a">
 	<li>3 new context variables:
<ul>
 	<li><span>_selectedCount</span>: number of clicks in the selection. It starts at zero, goes to 1 when a day is selected, goes to 2 if another day is selected and back to 0 when the range is cleared</li>
 	<li><span>_selectionStart</span>: The date in the selection (the selected range is FROM this value)</li>
 	<li><span>_selectionEnd</span>: The last date in the selection (the selected range is UP TO AND INCLUDING this value)</li>
</ul>
</li>
 	<li>One new circle in the gallery to show selections</li>
 	<li>New OnSelect formula on the <strong>TextDay</strong> TextBox to update the new context variables</li>
</ol>
&nbsp;
<h2>Adding the OnSelect formula to handle taps</h2>
<ul>
 	<li>Select the TextBox <strong>TextDay</strong> by clicking on the first item of the main gallery. Then set this formula:</li>
</ul>
<pre><strong>TextDay.OnSelect =</strong>
If( Month(DateAdd(_firstDayInView,ThisItem.Value,Days))&lt;&gt;Month(_firstDayOfMonth),
    0,

    _selectedCount &gt; 0
    &amp;&amp; DateAdd(_firstDayInView,ThisItem.Value,Days) &gt;= _selectionStart
    &amp;&amp; DateAdd(_firstDayInView,ThisItem.Value,Days) &lt;= _selectionEnd,
    UpdateContext({_selectedCount: 0}),

    _selectedCount = 1,
    UpdateContext({_selectionStart: Min(_selectionStart, DateAdd(_firstDayInView,ThisItem.Value,Days)),
                   _selectionEnd:   Max(_selectionStart, DateAdd(_firstDayInView,ThisItem.Value,Days)),
                   _selectedCount:  2}),

    UpdateContext({_selectionStart: DateAdd(_firstDayInView,ThisItem.Value,Days),
                   _selectionEnd:   DateAdd(_firstDayInView,ThisItem.Value,Days),
                   _selectedCount:  1}))
</pre>
Let’s go through this in pieces. We first check if the current day is in the current month. If it isn’t, we simply say <span>0</span> and do nothing. The return value of an action formula such as OnSelect is irrelevant, and we are using <span>0</span> just because it is the shortest syntactically valid option.

Otherwise (i.e. the clicked day is in the current month), then we go through a series of conditions:
<ul>
 	<li>Is the clicked date part of a current selection range? We see if there is a selection (<span>_selectedCount &gt; 0</span>) and the clicked date is at or after the beginning of the range and at or before the end of the range. If so, we clear the selection (<span>UpdateContext({_selectedCount: 0})</span>)</li>
 	<li>Was a single day already selected? We check this via <span>_selectedCount=1</span>. If so, we update the start to be the lesser of the clicked date and the previously selected date, and similary set the end of the selection to be the greater of the two. We also update <span>_selectedCount</span> to 2, indicating that a range of multiple dates is selected.</li>
</ul>
If none of the above (i.e. the clicked day is in the current month and it is not part of a selection), then set the current day as the only selected date.

&nbsp;
<h2>Showing the current selection</h2>
We have everything in place to select dates and ranges, but we aren’t showing it to your users yet. Let’s address that that now.
<ul>
 	<li>Select the <strong>CalendarGallery</strong> template by clicking on the first item of this gallery</li>
 	<li>Add a circle ("Insert" in the ribbon --&gt; "Icons" --&gt; "Circle") and rename it to <strong>DaySelectedCircle</strong></li>
</ul>
<pre><strong>DaySelectedCircle.Visible =</strong>
_selectedCount &gt; 0
&amp;&amp; DateAdd(_firstDayInView,ThisItem.Value,Days) &gt;= _selectionStart
&amp;&amp; DateAdd(_firstDayInView,ThisItem.Value,Days) &lt;= _selectionEnd

<strong>DaySelectedCircle.Fill =</strong>
ColorFade(
    RGBA(48,128,255,1),
    If( Month(DateAdd(_firstDayInView,ThisItem.Value,Days))=Month(_firstDayOfMonth),
        0,
        80%)
)
</pre>
Here is what you should see at this point if you select a date range.

<a href="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image8.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image8.png" alt="cal-image8" width="277" height="263" class="alignnone size-full wp-image-166" /></a>
Notice that again the dark text is hard to read on top of selected dates. We already fixed that for holidays, so we now need to modify TextDay.Color as follows:
<pre><strong>TextDay.Color =</strong>
ColorFade(
    If( HolidayCircle.Visible <strong>|| DaySelectedCircle.Visible</strong>,
        RGBA(255,255,255,1),
        RGBA(47,41,43,1)),
    If( Month(DateAdd(_firstDayInView,ThisItem.Value,Days))=Month(_firstDayOfMonth),
        0,
        80%)
)
</pre>
Notice that now we are using white if the day is a holiday OR if it is selected (we only added the <span>|| DaySelectedCircle.Visible</span> condition).

&nbsp;
<h2>Finishing touches</h2>
<ul>
 	<li>Add 2 rectangles and place them behind the main gallery on the left and on the right to indicate weekend.
<ul>
 	<li>To place them behind the gallery, click “Home” in the ribbon à “Reorder” à “Send to back”</li>
 	<li>Set the Fill property to RGBA(0,0,0,0.05). This is a very light shade of gray</li>
</ul>
</li>
 	<li>Make your <strong>DaySelectedCircle</strong> slightly larger than <strong>HolidayCircle</strong> and send it to back
<ul>
 	<li>This will allow your users to select a day even if it is a holiday, and they will be able to see selected holidays</li>
</ul>
</li>
 	<li>Highlight Today’s date by making it Bold and underlined:</li>
</ul>
<pre>
<strong>TextDay.FontWeight =</strong>
If(IsToday(DateAdd(_firstDayInView,ThisItem.Value,Days)),FontWeight.Bold,FontWeight.Normal)

<strong>TextDay.Underline =</strong> IsToday(DateAdd(_firstDayInView,ThisItem.Value,Days))
</pre>
We are using the <span>IsToday</span> function, which does exactly what its name indicates. Here is what you should get:

<a href="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image9.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/cal-image9.png" alt="cal-image9" width="278" height="275" class="alignnone size-full wp-image-215" /></a>

&nbsp;
<h2>Wrapping up</h2>
Congratulations if you made it this far, and hopefully you had some fun while learning a thing or two. Keep sending us feedback and interacting with us on our <a href="https://powerusers.microsoft.com/t5/PowerApps-Community/ct-p/PowerApps1">forums</a>. Tell us what you like and don’t like, and together we will keep pushing the boundaries of what is possible.

That’s all for today. See you next time.]]></content:encoded>
    </item>
    <item>
      <title>Source Maps under the hood – VLQ, Base64 and Yoda</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/source-maps-under-the-hood-vlq-base64-and-yoda</link>
      <pubDate>Mon, 14 Mar 2016 09:56:49 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/?p=143</guid>
      <description><![CDATA[Source Maps can be extremely useful whenever there is a preprocessor that modifies an input into its...]]></description>
      <content:encoded><![CDATA[Source Maps can be extremely useful whenever there is a preprocessor that modifies an input into its corresponding output. In such cases, a source map enables one to figure out the original location (line, column) that corresponds to a given location of the output (line, column), and vice-versa.

This article discusses source maps, the problem they try to solve, how they work and some of the why it is the way it is. If I am successful in my attempt to explain things, by the end of the article you will have all the tools you need to understand or create your own source maps – even by hand if you want to!
<!--more-->
<blockquote><strong>Note:</strong> This article won’t be very useful for the vast majority of people who simply want to <em>use</em> source maps as opposed to <em>understand</em> source maps inside and out. If you don’t care about the details of <em>how</em> it works, then this article is probably not for you.</blockquote>
Why Yoda in the title of the article, you ask? Well, for the purposes of this article we will treat Yoda as a Jedi text preprocessor that takes words as inputs and puts them in semi-random order as output. TypeScript and CoffeScript are more complex preprocessors (although not necessarily Jedi), but the idea remains:
<blockquote>Input &rArr; Preprocessor &rArr; Output</blockquote>
<blockquote><strong>Disclaimer:</strong> I am NOT an authoritative source on Source Maps and everything in this article is based on other articles I read online and on my own experimentation. If something looks wrong, it might very well be. Please leave a comment below and I’ll try to address it. My hope is that this will help explain some of the gaps not thoroughly covered in other places.</blockquote>
<h1>Who uses Source Maps?</h1>
Notable cases where source maps are commonly used include JavaScript compilers / transpilers such as CoffeeScript and TypeScript, or CSS preprocessors such as Less. All modern browsers support source maps to ease debugging: Chrome, Firefox, Safari, Edge and Internet Explorer 11 (since Windows 8.1 Update).

Here’s an excerpt describing the benefits, from <a href="https://msdn.microsoft.com/en-us/library/dn255007(v=vs.85).aspx#source_maps" target="_blank">here</a>
<blockquote><em>When the compiled or compressed JavaScript contains a pointer to a source map, all files are present, and source map functionality is enabled, the Debugger tool shows the original source and maps breakpoints and errors to it.</em></blockquote>
With Visual Studio 2013 and up you can even debug TypeScript right within the IDE (when you hit F5 and choose IE as the browser). The ability to set breakpoint and control execution flow in the original TypeScript source is a very powerful feature, and the magic behind that lies in Source Maps. You can achieve most of the same awesomeness on other browsers as well by following a few steps to get source maps generated for you, but IE still provides by far the best debugging experience for TypeScript (I suppose IE doesn’t get that kind of praise too often, which is unfortunate). See <a href="https://www.gamefromscratch.com/post/2014/05/27/TypeScript-debugging-in-Visual-Studio-with-IE-Chrome-and-Firefox-using-Source-Maps.aspx" target="_blank">Typescript debugging in Visual Studio with IE, Chrome and Firefox using source maps</a>.
<h1>The most basic source map</h1>
One way to create a source map would be to simply store the source location (line, column) that corresponds to each output character’s location.
<blockquote>“feel the force” &rArr; Yoda &rArr; “the force feel”</blockquote>
<table width="600" border="1">
<tbody>
<tr>
<th>Output location</th>
<th>Input</th>
<th>Input location</th>
<th>Character</th>
</tr>
<tr>
<td>Line 1, Column 0</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 5</td>
<td>t</td>
</tr>
<tr>
<td>Line 1, Column 1</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 6</td>
<td>h</td>
</tr>
<tr>
<td>Line 1, Column 2</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 7</td>
<td>e</td>
</tr>
<tr>
<td>Line 1, Column 4</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 9</td>
<td>f</td>
</tr>
<tr>
<td>Line 1, Column 5</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 10</td>
<td>o</td>
</tr>
<tr>
<td>Line 1, Column 6</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 11</td>
<td>r</td>
</tr>
<tr>
<td>Line 1, Column 7</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 12</td>
<td>c</td>
</tr>
<tr>
<td>Line 1, Column 8</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 13</td>
<td>e</td>
</tr>
<tr>
<td>Line 1, Column 10</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 0</td>
<td>f</td>
</tr>
<tr>
<td>Line 1, Column 11</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 1</td>
<td>e</td>
</tr>
<tr>
<td>Line 1, Column 12</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 2</td>
<td>e</td>
</tr>
<tr>
<td>Line 1, Column 13</td>
<td>Yoda_input.txt</td>
<td>Line 1, Column 3</td>
<td>l</td>
</tr>
</tbody>
</table>
&nbsp;
This table could then be encoded like this (the colors below are used simply to make it easier to distinguish one mapping from the next):
<blockquote><strong>Mappings</strong> (283 chars length): <span style="color: #ff0000">1|0|Yoda_input.txt|1|5</span>, <span style="color: #0000ff">1|1|Yoda_input.txt|1|6</span>, <span style="color: #ff0000">1|2|Yoda_input.txt|1|7</span>, <span style="color: #0000ff">1|4|Yoda_input.txt|1|9</span>, <span style="color: #ff0000">1|5|Yoda_input.txt|1|10</span>, <span style="color: #0000ff">1|6|Yoda_input.txt|1|11</span>, <span style="color: #ff0000">1|7|Yoda_input.txt|1|12</span>, <span style="color: #0000ff">1|8|Yoda_input.txt|1|13</span>, <span style="color: #ff0000">1|10|Yoda_input.txt|1|0</span>, <span style="color: #0000ff">1|11|Yoda_input.txt|1|1</span>, <span style="color: #ff0000">1|12|Yoda_input.txt|1|2</span>, <span style="color: #0000ff">1|13|Yoda_input.txt|1|3</span></blockquote>
While this would work, the source map grows quickly and takes up a lot of space. Let’s see how we can improve it.
<h2>1. Improvement: Don’t write output line number each time. Use ‘;’ to indicate a line change</h2>
Because in many cases the output is condensed into fewer lines than the input (minified JavaScript for example), not having to write the output line location for each entry results in great space savings. The same table we had before can now be encoded as follows:
<blockquote><strong>Mappings</strong> (245 chars length): <span style="color: #ff0000">0|Yoda_input.txt|1|5</span>, <span style="color: #0000ff">1|Yoda_input.txt|1|6</span>, <span style="color: #ff0000">2|Yoda_input.txt|1|7</span>, <span style="color: #0000ff">4|Yoda_input.txt|1|9</span>, <span style="color: #ff0000">5|Yoda_input.txt|1|10</span>, <span style="color: #0000ff">6|Yoda_input.txt|1|11</span>, <span style="color: #ff0000">7|Yoda_input.txt|1|12</span>, <span style="color: #0000ff">8|Yoda_input.txt|1|13</span>, <span style="color: #ff0000">10|Yoda_input.txt|1|0</span>, <span style="color: #0000ff">11|Yoda_input.txt|1|1</span>, <span style="color: #ff0000">12|Yoda_input.txt|1|2</span>, <span style="color: #0000ff">13|Yoda_input.txt|1|3</span>;</blockquote>
<h2>2. Improvement: Table of names and inputs, referencing by indexes</h2>
<table width="300" border="1">
<tbody>
<tr>
<th>Index</th>
<th>Input</th>
</tr>
<tr>
<td>0</td>
<td>Yoda_input.txt</td>
</tr>
</tbody>
</table>
&nbsp;
<table width="300" border="1">
<tbody>
<tr>
<th>Index</th>
<th>Name</th>
</tr>
<tr>
<td>0</td>
<td>the</td>
</tr>
<tr>
<td>1</td>
<td>force</td>
</tr>
<tr>
<td>2</td>
<td>feel</td>
</tr>
</tbody>
</table>
&nbsp;
<table width="700" border="1">
<tbody>
<tr>
<th>Output location</th>
<th>Input index</th>
<th>Input location</th>
<th>Name index</th>
</tr>
<tr>
<td>Line 1, Column 0</td>
<td>0</td>
<td>Line 1, Column 5</td>
<td>0</td>
</tr>
<tr>
<td>Line 1, Column 4</td>
<td>0</td>
<td>Line 1, Column 9</td>
<td>1</td>
</tr>
<tr>
<td>Line 1, Column 10</td>
<td>0</td>
<td>Line 1, Column 0</td>
<td>2</td>
</tr>
</tbody>
</table>
&nbsp;
Now this can be encoded as:
<blockquote><strong>Inputs:</strong> Yoda_input.txt
<strong>Names:</strong> the,force,feel
<strong>Mappings</strong> (31 chars length): <span style="color: #ff0000">0|0|1|5|0</span>, <span style="color: #0000ff">4|0|1|9|1</span>, <span style="color: #ff0000">10|0|1|0|2</span>;</blockquote>
<h2>3. Improvement: Use relative (instead of absolute) offsets</h2>
This idea will only be useful when leveraged by improvement 4 (VLQ encoding - Variable length Quantities). The goal here is to try to make the numbers that we encode smaller, because we can encode smaller numbers more efficiently than larger numbers thanks to VLQ.

The way we try to make most of the numbers smaller is by using relative offsets. Looking at the first column of the previous table, here’s what relative offsets mean:
<table width="400" border="1">
<tbody>
<tr>
<th>Output location
(improvement 2)</th>
<th>Output location
(improvement 3)</th>
</tr>
<tr>
<td>Line 1, Column 0</td>
<td>Line 1, Column <strong><span style="text-decoration: underline">0</span></strong></td>
</tr>
<tr>
<td>Line 1, Column 4</td>
<td>Line 1, Column (last + <strong><span style="text-decoration: underline">4</span></strong> = 4)</td>
</tr>
<tr>
<td>Line 1, Column 10</td>
<td>Line 1, Column (last + <strong><span style="text-decoration: underline">6</span></strong> = 10)</td>
</tr>
</tbody>
</table>
&nbsp;
Applying that to the entire table, we end up with:
<table width="600" border="1">
<tbody>
<tr>
<th>Output location</th>
<th>Input index</th>
<th>Input location</th>
<th>Name index</th>
</tr>
<tr>
<td>Line 1, Column 0</td>
<td>0</td>
<td>Line 1, Column 5</td>
<td>0</td>
</tr>
<tr>
<td>Line 1, Column +4</td>
<td>+0</td>
<td>Line 1, Column +4</td>
<td>+1</td>
</tr>
<tr>
<td>Line 1, Column +6</td>
<td>+0</td>
<td>Line 1, Column -9</td>
<td>+1</td>
</tr>
</tbody>
</table>
&nbsp;
And this can now be encoded as:
<blockquote><strong>Inputs:</strong> Yoda_input.txt
<strong>Names:</strong> the,force,feel
<strong>Mappings</strong> (31 chars length): <span style="color: #ff0000">0|0|1|5|0</span>, <span style="color: #0000ff">4|0|1|4|1</span>, <span style="color: #ff0000">6|0|1|-9|1</span>;</blockquote>
<h2>4. Improvement: VLQ (Variable Length Quantities)</h2>
<h3>4.1. VLQ in the decimal system</h3>
The idea behind VLQ is rather simple. Imagine you wanted to represent 4 decimal number in order. One option is to use a special character to separate those numbers:
<blockquote>1|2|3|4</blockquote>
What if you knew ahead of time that the numbers are all single digits? In that case you don’t need the separator characters, and the following would still be perfectly understandable, but much shorter (half the number of characters in this case):
<blockquote>1234</blockquote>
But what if each number could be more than a single digit? Well, with VLQ all you have to do is add an indicator to digits to indicate that the number still has more digits. Let’s encode the following numbers using VLQ:
<blockquote>1|23|456|7</blockquote>
Using an underline as our indicator that a number has more digits (the continuation indicator):
<blockquote>1<span style="text-decoration: underline">2</span>3<span style="text-decoration: underline">45</span>67</blockquote>
Here’s how to read this:
<ul>
	<li>1 does not have an underscore, so that’s it → The first number is 1</li>
	<li>2 has an underscore, so we continue; 3 does not, so that’s it → The second number is 23</li>
	<li>4 has an underscore, so we continue; 5 also has an underscore, so we continue; 6 does not, so that’s it → The third number is 456</li>
	<li>7 does not have an underscore, so that’s it → The fourth number is 7</li>
</ul>
<h3>4.2. VLQ in binary form</h3>
I cheated a little bit on the example above with the decimal system. I introduced the underscores as the continuation indicator, but underscores are, of course, not part of the decimal system. Introducing underscores is effectively the same as using a base-20 system instead (there are now 20 possible values for a digit: 0 – 9 not underlined and 0 – 9 underlined).

In binary, however, we don’t have to cheat. How nice! Instead, we will use binary groups made of 6 bits (for a total of 64 possible values), and we will use one of those bits as the continuation indicator (‘C’ below). While at it, let’s also reserve another bit as a sign indicator (‘S’ below). ‘0’ will indicate positive; ‘1’ will indicate negative.

<strong>First binary group (4 bits for value):</strong>
<table width="300" border="1">
<tbody>
<tr>
<th style="text-align: center">B<sub>5</sub></th>
<th style="text-align: center">B<sub>4</sub></th>
<th style="text-align: center">B<sub>3</sub></th>
<th style="text-align: center">B<sub>2</sub></th>
<th style="text-align: center">B<sub>1</sub></th>
<th style="text-align: center">B<sub>0</sub></th>
</tr>
<tr>
<td style="text-align: center">C</td>
<td style="text-align: center" colspan="4">Value</td>
<td style="text-align: center">S</td>
</tr>
</tbody>
</table>
&nbsp;
Here’s what each of those binary ‘groups’ can represent:
<table width="500" border="1">
<tbody>
<tr>
<th>Binary group</th>
<th>Meaning</th>
</tr>
<tr>
<td>000000</td>
<td>0</td>
</tr>
<tr>
<td>000001 *</td>
<td>-0</td>
</tr>
<tr>
<td>000010</td>
<td>1</td>
</tr>
<tr>
<td>000011</td>
<td>-1</td>
</tr>
<tr>
<td>000100</td>
<td>2</td>
</tr>
<tr>
<td>000101</td>
<td>-2</td>
</tr>
<tr>
<td>…</td>
<td>…</td>
</tr>
<tr>
<td>011110</td>
<td>15</td>
</tr>
<tr>
<td>011111</td>
<td>-15</td>
</tr>
<tr>
<td>100000</td>
<td>0 with continuation</td>
</tr>
<tr>
<td>100001</td>
<td>-0 with continuation</td>
</tr>
<tr>
<td>100010</td>
<td>1 with continuation</td>
</tr>
<tr>
<td>100011</td>
<td>-1 with continuation</td>
</tr>
<tr>
<td>…</td>
<td>…</td>
</tr>
<tr>
<td>111110</td>
<td>15 with continuation</td>
</tr>
<tr>
<td>111111</td>
<td>-15 with continuation</td>
</tr>
</tbody>
</table>
* Not required, but technically possible

There is one additional improvement we can make: when a binary group has a continuation, the next binary groups don’t need to specify the sign bit again. We already know the sign from the first group, so we can use up to 5 bits for the value. It still needs the continuation bit, though, because the numbers VLQ encodes could be arbitrarily large and might require more than 2 binary groups to be represented.

<strong>Continuation binary group (5 bits for value):</strong>
<table width="300" border="1">
<tbody>
<tr>
<th style="text-align: center">B<sub>5</sub></th>
<th style="text-align: center">B<sub>4</sub></th>
<th style="text-align: center">B<sub>3</sub></th>
<th style="text-align: center">B<sub>2</sub></th>
<th style="text-align: center">B<sub>1</sub></th>
<th style="text-align: center">B<sub>0</sub></th>
</tr>
<tr>
<td style="text-align: center">C</td>
<td style="text-align: center" colspan="5">Value</td>
</tr>
</tbody>
</table>
&nbsp;
Let’s now encode the same numbers we had before, but in binary: 1|23|456|7.

Helper table for decimal to binary conversions:
<table width="400" border="1">
<tbody>
<tr>
<th>Decimal</th>
<th>Binary</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>23</td>
<td>10111</td>
</tr>
<tr>
<td>456</td>
<td>111001000</td>
</tr>
<tr>
<td>7</td>
<td>111</td>
</tr>
</tbody>
</table>
&nbsp;
<h4>Encoding decimal 1:</h4>
The corresponding binary value takes up a single bit (<span style="color: #0000ff">1</span>). This will easily fit inside the first binary group (it has space for 4 bits). So this is what we get:
<table width="300" border="1">
<tbody>
<tr>
<th style="text-align: center">B<sub>5</sub>
(C)</th>
<th style="text-align: center">B<sub>4</sub></th>
<th style="text-align: center">B<sub>3</sub></th>
<th style="text-align: center">B<sub>2</sub></th>
<th style="text-align: center">B<sub>1</sub></th>
<th style="text-align: center">B<sub>0</sub>
(S)</th>
</tr>
<tr>
<td style="text-align: center">0</td>
<td style="text-align: center"><span style="color: #0000ff">0</span></td>
<td style="text-align: center"><span style="color: #0000ff">0</span></td>
<td style="text-align: center"><span style="color: #0000ff">0</span></td>
<td style="text-align: center"><span style="color: #0000ff">1</span></td>
<td style="text-align: center">0</td>
</tr>
</tbody>
</table>
&nbsp;
<h4>Encoding decimal 23:</h4>
The corresponding binary value takes up 5 bits (<span style="color: #ff0000">1</span><span style="color: #0000ff">0111</span>), so it doesn’t fit in the first binary group and therefore requires a continuation. We will put the 4 least-significant bits in the first group (<span style="color: #0000ff">0111</span>) and leave the rest (<span style="color: #ff0000">1</span>) for the continuation group.
<table width="650" border="1">
<tbody>
<tr>
<th style="text-align: center">B<sub>5</sub>
(C)</th>
<th style="text-align: center">B<sub>4</sub></th>
<th style="text-align: center">B<sub>3</sub></th>
<th style="text-align: center">B<sub>2</sub></th>
<th style="text-align: center">B<sub>1</sub></th>
<th style="text-align: center">B<sub>0</sub>
(S)</th>
<th width="50"></th>
<th style="text-align: center">B<sub>5</sub>
(C)</th>
<th style="text-align: center">B<sub>4</sub></th>
<th style="text-align: center">B<sub>3</sub></th>
<th style="text-align: center">B<sub>2</sub></th>
<th style="text-align: center">B<sub>1</sub></th>
<th style="text-align: center">B<sub>0</sub></th>
</tr>
<tr>
<td style="text-align: center">1</td>
<td style="text-align: center"><span style="color: #0000ff">0</span></td>
<td style="text-align: center"><span style="color: #0000ff">1</span></td>
<td style="text-align: center"><span style="color: #0000ff">1</span></td>
<td style="text-align: center"><span style="color: #0000ff">1</span></td>
<td style="text-align: center">0</td>
<td></td>
<td style="text-align: center">0</td>
<td style="text-align: center"><span style="color: #ff0000">0</span></td>
<td style="text-align: center"><span style="color: #ff0000">0</span></td>
<td style="text-align: center"><span style="color: #ff0000">0</span></td>
<td style="text-align: center"><span style="color: #ff0000">0</span></td>
<td style="text-align: center"><span style="color: #ff0000">1</span></td>
</tr>
</tbody>
</table>
&nbsp;
<h4>Encoding decimal 456:</h4>
The corresponding binary value takes up 9 bits (<span style="color: #ff0000">11100</span><span style="color: #0000ff">1000</span>), so it doesn’t fit in the first binary group and therefore requires a continuation. We will put the 4 least-significant bits in the first group (<span style="color: #0000ff">1000</span>) and leave the rest (<span style="color: #ff0000">11100</span>) for the continuation group.
<table width="650" border="1">
<tbody>
<tr>
<th style="text-align: center">B<sub>5</sub>
(C)</th>
<th style="text-align: center">B<sub>4</sub></th>
<th style="text-align: center">B<sub>3</sub></th>
<th style="text-align: center">B<sub>2</sub></th>
<th style="text-align: center">B<sub>1</sub></th>
<th style="text-align: center">B<sub>0</sub>
(S)</th>
<th width="50"></th>
<th style="text-align: center">B<sub>5</sub>
(C)</th>
<th style="text-align: center">B<sub>4</sub></th>
<th style="text-align: center">B<sub>3</sub></th>
<th style="text-align: center">B<sub>2</sub></th>
<th style="text-align: center">B<sub>1</sub></th>
<th style="text-align: center">B<sub>0</sub></th>
</tr>
<tr>
<td style="text-align: center">1</td>
<td style="text-align: center"><span style="color: #0000ff">1</span></td>
<td style="text-align: center"><span style="color: #0000ff">0</span></td>
<td style="text-align: center"><span style="color: #0000ff">0</span></td>
<td style="text-align: center"><span style="color: #0000ff">0</span></td>
<td style="text-align: center">0</td>
<td></td>
<td style="text-align: center">0</td>
<td style="text-align: center"><span style="color: #ff0000">1</span></td>
<td style="text-align: center"><span style="color: #ff0000">1</span></td>
<td style="text-align: center"><span style="color: #ff0000">1</span></td>
<td style="text-align: center"><span style="color: #ff0000">0</span></td>
<td style="text-align: center"><span style="color: #ff0000">0</span></td>
</tr>
</tbody>
</table>
&nbsp;
<h4>Encoding decimal 7:</h4>
The corresponding binary value takes up 3 bits (<span style="color: #0000ff">111</span>). This will easily fit inside the first binary group (it has space for 4 bits). So this is what we get:
<table width="300" border="1">
<tbody>
<tr>
<th style="text-align: center">B<sub>5</sub>
(C)</th>
<th style="text-align: center">B<sub>4</sub></th>
<th style="text-align: center">B<sub>3</sub></th>
<th style="text-align: center">B<sub>2</sub></th>
<th style="text-align: center">B<sub>1</sub></th>
<th style="text-align: center">B<sub>0</sub>
(S)</th>
</tr>
<tr>
<td style="text-align: center">0</td>
<td style="text-align: center"><span style="color: #0000ff">0</span></td>
<td style="text-align: center"><span style="color: #0000ff">1</span></td>
<td style="text-align: center"><span style="color: #0000ff">1</span></td>
<td style="text-align: center"><span style="color: #0000ff">1</span></td>
<td style="text-align: center">0</td>
</tr>
</tbody>
</table>
&nbsp;
<h4>The final binary result</h4>
Now all we have to do is concatenate the results we have so far:
<blockquote>000010 101110 000001 110000 011100 001110</blockquote>
<h4>Base64 encoding</h4>
As it turns out, traditional <a href="https://en.wikipedia.org/wiki/Base64" target="_blank">base64</a> encoding defines an alphabet of 64 characters. Values from 0 – 63 can be encoded as a single character. Well, the initial choice to use 6 bits per binary group was not arbitrary. By using 6 bits per group we can now encode each group as a single character as per the base 64 alphabet. Here’s what we get for the bits above:
<blockquote>CuBwcO</blockquote>
<h3>4.3. Applying VLQ + base64 to the Yoda example</h3>
Following the same steps as outlined above, we can now encode what we had at the end of Improvement 3 by using VLQ + base64. This is what we get:
<blockquote><strong>Inputs:</strong> Yoda_input.txt
<strong>Names:</strong> the,force,feel
<strong>Mappings</strong> (18 chars length): <span style="color: #ff0000">AACKA</span>, <span style="color: #0000ff">IACIC</span>, <span style="color: #ff0000">MACTC</span>;</blockquote>
<h2>5. Improvement: Omit unnecessary fields</h2>
A mapping can omit some of the fields if those fields don’t provide any useful information. This means that a mapping entry can be any of the following options:
<ul>
	<li>5 fields: Output column, input file name index, input line, input column, name index</li>
	<li>4 fields: Output column, input file name index, input line, input column</li>
	<li>1 field: Output column</li>
</ul>
Sadly, the simplistic Yoda example in this article wouldn’t benefit from this.
<h1>Wrapping up</h1>
Here’s a sample source map for the Yoda example, which should no longer be scary to look at.
<code>{
&nbsp;&nbsp;&nbsp;&nbsp;"version": 3,
&nbsp;&nbsp;&nbsp;&nbsp;"file": "Yoda_output.txt",
&nbsp;&nbsp;&nbsp;&nbsp;"sources": ["Yoda_input.txt"],
&nbsp;&nbsp;&nbsp;&nbsp;"names": ["the", "force", "feel"],
&nbsp;&nbsp;&nbsp;&nbsp;"mappings": "AACKA,IACIC,MACTC;"
}</code>

Pretty cool, right? Next time you see Yoda, remember to ask him for a Source Map. You can also use an online tool by Alexander Pavlov available <a href="https://murzwin.com/base64vlq.html" target="_blank">here</a> to play with VLQ + base64 and see it working in real time.

Let me know what you think in the comments and I will see you next time.
<h2>Useful links</h2>
<ul>
	<li><a href="https://docs.google.com/a/google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#" target="_blank">Source Map Revision 3 Proposal</a> (official)</li>
	<li><a href="https://murzwin.com/base64vlq.html" target="_blank">Online tool to play with VLQ + base 64 encodings</a></li>
	<li><a href="https://www.thecssninja.com/javascript/source-mapping" target="_blank">Another article</a> that tries to explain Source Maps and VLQ encoding.</li>
	<li><a href="https://github.com/mozilla/source-map" target="_blank">Library to generate and consume source maps</a> on GitHub</li>
</ul>]]></content:encoded>
    </item>
    <item>
      <title>WinRT XAML for Serious Beginners – The XML in XAML</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/winrt-xaml-for-serious-beginners-the-xml-in-xaml</link>
      <pubDate>Mon, 19 Jan 2015 21:16:00 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/2015/01/19/winrt-xaml-for-serious-beginners-the-xml-in-xaml/</guid>
      <description><![CDATA[What is XAML  XAML is XML with a letter ‘A’ in it. Let’s learn XML first before we can add that very...]]></description>
      <content:encoded><![CDATA[<h3>What is XAML</h3>  <p>XAML is XML with a letter ‘A’ in it. Let’s learn XML first before we can add that very important ‘A’.</p>  <p>It is important to note that XAML uses a feature of XML that many people are not familiar with: <strong>XML namespaces</strong>. Read on to see a very brief explanation of XML namespaces.</p>  <h3>Let’s learn XML, then!</h3>  <p>Here’s one of the infinite ways one could describe the <a href="https://blogs.msdn.com/b/davidni/archive/2015/01/20/winrt-xaml-for-serious-beginners-intro.aspx">first post</a> from this series in XML (eXtensible Markup Language):</p>  <pre><code class="xml">&lt;BlogPost Title=&quot;WinRT XAML for Serious Beginners – Intro&quot;
          Author=&quot;David Nissimoff&quot;&gt;
    &lt;Section Header=&quot;&quot;&gt;
        Hello everyone! (...)
    &lt;/Section&gt;
    &lt;Section Header=&quot;Why even consider learning XAML?&quot;&gt;
        (...)
    &lt;/Section&gt;
    (...)
    &lt;Ratings AllowAnnonymous=&quot;true&quot;&gt;&lt;/Ratings&gt;&lt;!--no ratings yet--&gt;
&lt;/BlogPost&gt;</code></pre>

<p>Looking closely, you’ll see the snippet above has that article’s text in it, but there’s also some other stuff between ‘&lt;’, ‘&gt;’ characters. These are XML tags. There’s the opening tag &lt;BlogPost&gt; and the matching closing tag &lt;/BlogPost&gt;. Note also how the blog post contains <strong>sections</strong>, indicated by opening tags &lt;Section&gt; and closing tags &lt;/Section&gt;. Opening tags can also have attributes, so a generic opening tag could be &lt;SomeTagName SomeAttribute1=&quot;Attribute value&quot; SomeAttribute2=&quot;Other value&quot;&gt;.</p>

<p>It is worth pointing out how the &lt;Ratings&gt; element doesn’t have any content (the closing tag immediately follows the opening tag). If you don’t have anything to put between the opening and closing tag, you can save some typing by using the shorter, self-closing notation &lt;Ratings AllowAnnonymous=&quot;true&quot;/&gt;. There are no other semantic differences and the latter is shorter, and hence usually preferred whenever possible.</p>

<p>You might also have noticed that this snippet had a comment in it: &lt;!--no ratings yet--&gt;. Comments start with “&lt;!--&quot; and end with “--&gt;”. An empty comment could be written as “&lt;!----&gt;”</p>

<h3>Fancy XML – Namespaces</h3>

<p>Now that you know about XML tags, the previous XML snippet describing this blog post should be easily understandable and its structure very clean. However, the only reason why you actually know what that snippet is describing, is because you’re referring to some common knowledge and assuming that &lt;BlogPost&gt; refers to a blog post, that &lt;Section&gt; refers to a text section within the blog post, etc.</p>

<p>Note, however, that &lt;Section&gt; could mean other things in different contexts. If I’d been describing an orchestra (stay with me!) – in this case the orchestra has a <em>brass section </em>and a <em>string section </em>– then the following XML might be reasonable:</p>

<pre><code class="xml">&lt;Orchestra Name=&quot;My Nameless Orchestra&quot;&gt;
    &lt;Section Type=&quot;Brass&quot; /&gt;
    &lt;Section Type=&quot;String&quot; /&gt;
&lt;/Orchestra&gt;</code></pre>

<p>Now we have a problem: &lt;Section&gt; means two completely different things in these contexts, and <strong>computers don’t like ambiguity</strong> (which is a good thing – we sure don’t want a section of this blog post ending up in an orchestra!). So let’s be more explicit about what we mean.</p>

<pre><code class="xml">&lt;BlogPost xmlns=&quot;blog post common knowledge&quot;
          Title=&quot;XAML for Serious Beginners&quot;
          Author=&quot;David Nissimoff&quot;&gt;
    &lt;Section Header=&quot;&quot;&gt;
        Hello everyone! (...)
    &lt;/Section&gt;
    &lt;Section Header=&quot;Why even consider learning XAML?&quot;&gt;
        (...)
    &lt;/Section&gt;
    (...)
    &lt;Ratings AllowAnnonymous=&quot;true&quot;&gt;&lt;/Ratings&gt;&lt;!--no ratings yet--&gt;
&lt;/BlogPost&gt;</code></pre>

<p>The important part here is the attribute <strong>xmlns</strong> (<strong>XML</strong> <strong>N</strong>ame<strong>S</strong>pace declaration). The value of this attribute can be any string – XML does very little to enforce any format. It is customary, however, to use strings that <strong>look like web URI’s</strong>, which don’t even have to exist! XAML uses this by default: xmlns=&quot;https://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;. Try going there with a browser, and you’ll see<strong> it is NOT a valid link – and this is totally fine</strong>.</p>

<p>You can also define more namespaces in one XML document, by using prefixes. Consider a blog post about XAML for orchestra members:</p>

<pre><code class="xml">&lt;BlogPost xlmns=&quot;blog post common knowledge&quot;
          xmlns:orch=&quot;orchestra common knowledge&quot;
          Title=&quot;XAML for Orchestra members&quot;
          Author=&quot;Not Me&quot;&gt;
    &lt;Section Header=&quot;Introduction&quot;&gt;
        This is some text in the blog post.
    &lt;/Section&gt;
    &lt;orch:Orchestra Name=&quot;My Nameless Orchestra&quot;&gt;
        &lt;orch:Section Type=&quot;Brass&quot; /&gt;
        &lt;orch:Section Type=&quot;String&quot; /&gt;
    &lt;/orch:Orchestra&gt;
&lt;/BlogPost&gt;</code></pre>

<p>Let’s see what just happened:</p>

<ul>
  <li>The &lt;BlogPost&gt; element defined the default namespace: “blog post common knowledge”. It is also defining a “xmlns:orch” namespace as “orchestra common knowledge”. Now that it is defined, this orch: namespace could be used in any of this tag’s children (it could also be used for the current tag!). </li>

  <li>The BlogPost has a text section with header “Introduction”. </li>

  <li>The blog post also has an orchestra (“orch:Orchestra”) which has a brass section and a string section. </li>

  <li>There is no more ambiguity! </li>
</ul>

<h3>So what *is* XAML?</h3>

<p>Now that we know the basics of XML, let’s move on to the ‘A’ in XAML: eXtensible <strong>Application</strong> Markup Language.</p>

<p>XAML is a declarative language that uses XML to describe how to initialize a set of objects. “Initializing a set of objects” is something that can be easily achieved with programming languages (such as C# or VB.NET) by calling class constructors and setting the desired properties. Indeed, <strong>almost everything you do with XAML can also be done in code</strong>. However, XAML offers a few significant advantages that will become clearer as we go:</p>

<ul>
  <li>Allows for cleaner / more maintainable code (because the <strong>UI design can be kept separate from code-behind</strong>) </li>

  <li>Requires much less typing that the equivalent C# code (it is <strong>more concise</strong>) </li>

  <li>Has very <strong>rich tooling support</strong> in Visual Studio: IntelliSense, code generation, F12 Go to Definition, etc. </li>

  <li>Supports <strong>Templating</strong>. I’ll discuss Templates and how to use them in a later post – suffice to say that there is a way to define a chunk of XAML as a template, and that template can be used multiple times in many places as if it was copied each time. Think of a Button control. There is a template that defines what it looks like, and then adding a button to a page will end up invoking the button Template. This is immensely useful for multiple scenarios, and one of XAML core strengths. </li>
</ul>

<h3>Other online resources</h3>

<ul>
  <li>W3Schools.com <a href="https://www.w3schools.com/xml/xml_syntax.asp">XML Syntax</a> </li>

  <li>W3Schools.com <a href="https://www.w3schools.com/xml/xml_namespaces.asp">XML namespaces</a> </li>
</ul>

<h3>Wrapping up</h3>

<p>This was meant as a jumpstart post into XML and the main XML features which are used extensively with XAML, and I hope it is useful at that. If you have any suggestions or feedback, please leave your comments below and I’ll do my best to address them in a timely manner.</p>]]></content:encoded>
    </item>
    <item>
      <title>WinRT XAML for Serious Beginners – Tips for successful online searches</title>
      <link>https://docs.microsoft.com/archive/blogs/davidni/winrt-xaml-for-serious-beginners-tips-for-successful-online-searches</link>
      <pubDate>Mon, 19 Jan 2015 20:14:00 GMT</pubDate>
      <dc:creator><![CDATA[David Nissimoff]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/davidni/2015/01/19/winrt-xaml-for-serious-beginners-tips-for-successful-online-searches/</guid>
      <description><![CDATA[This is a very short post, but at the same time it might be the most valuable of all. As the old...]]></description>
      <content:encoded><![CDATA[<p>This is a very short post, but at the same time it might be the most valuable of all. As the old saying goes:</p>  <blockquote>   <p><em>“Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.”</em></p> </blockquote>  <p><strong>This blog post does neither. But it shows you how to find other fishermen!</strong></p>  <h2>How to find WinRT XAML resources online</h2>  <p>Given the confusing and overlapping usage of terms (see my other post <a href="https://blogs.msdn.com/b/davidni/archive/2015/01/20/winrt-xaml-for-serious-beginners-where-are-we-navigating-win32-mfc-gdi-gdi-windows-forms-wpf-silverlight-windows-runtime-winrt-and-windows-phone.aspx">Where are we? Navigating Win32, MFC, GDI, GDI+, Windows Forms, WPF, Silverlight, Windows Runtime, WinRT and Windows Phone</a>, XAML is used in WPF as well as WinRT; WinRT could be a version of Windows or the API surface; etc.), here are some tips to help you get useful results online.</p>  <h3>A) Searching for MSDN references</h3>  <p>Using the query <strong>“windows runtime xaml &lt;what you’re looking for&gt;”</strong> (without quotes) usually works and finds what I need among the first results. For example:</p>  <ul>   <li>“windows runtime xaml EnableDependentAnimation” (<a href="https://www.bing.com/search?q=windows+runtime+xaml+EnableDependentAnimation">Bing</a>, <a href="https://www.google.com/search?q=windows+runtime+xaml+EnableDependentAnimation">Google</a>) </li>    <li>“windows runtime xaml themeresource markup” (<a href="https://www.bing.com/search?q=windows%20runtime%20xaml%20themeresource%20markup">Bing</a>, <a href="https://www.google.com/search?q=windows+runtime+xaml+themeresource+markup">Google</a>) </li> </ul>  <h3>B) Searching in Forums / Community resources</h3>  <p><strong>Adding the word “WinRT”</strong> in the search query will help you get meaningful results for modern apps since, more often than not, people in the community use WinRT to mean “Windows Runtime” rather than the specific Windows version.</p>  <ul>   <li>“winrt custom controls” (<a href="https://www.bing.com/search?q=winrt+custom+controls">Bing</a>, <a href="https://www.google.com/search?q=winrt+custom+controls">Google</a>) </li>    <li>“winrt animations” (<a href="https://www.bing.com/search?q=winrt%20animations">Bing</a>, <a href="https://www.google.com/search?q=winrt+animations">Google</a>) </li> </ul>  <h3>That’s it!</h3>  <p>If you have any suggestions or feedback, please leave your comments below and I’ll do my best to address them in a timely manner.</p>  <p>In the next post we will start learning XAML from the ground up: <a href="https://blogs.msdn.com/b/davidni/archive/2015/01/20/winrt-xaml-for-serious-beginners-the-xml-in-xaml.aspx">The XML in XAML</a>.</p>]]></content:encoded>
    </item>
  </channel>
</rss>