Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / XAML

And Now for XAML Completely Different

Rate me:
Please Sign up or sign in to vote.
4.92/5 (46 votes)
1 Apr 2010CPOL6 min read 43.4K   397   59  
Using Markup Extensions to build individual markup based declarative systems with XAML
<Main xmlns="clr-namespace:MarkupScript;assembly=MarkupScript">
  <!-- 
    Define some arguments to demonstrate automated help message. 
    If you call this without any arguments you will see a simple help screen. 
    You may also use -? or -help to show the help screen.
  -->
  <Main.ArgumentDefinitions>
    <ArgumentDefinitions>
      <!-- 
        Sequential arguments are strings on the command line, usually file names. 
        The order of sequential arguments on the commmand line is important. 
      -->
      <SequentialArgument Name="FirstArgument" Description="You must enter at least one argument"/>
      <!-- Sequential arguments are mandtory by default, we must explicitly declare them optional -->
      <SequentialArgument Name="SecondArgument" Description="You may enter a second argument" IsOptional="True"/>
      <!-- 
        Switches are preceeded by "-" or "/". Since they have a name, the order on the command line is not important.
        Switches are optional by default, so we must explicitly declare mandatory switches as not optional.
        If the switch can be assigned a value (like "/<Switch>=<Value>) we must set the HasValue attribute to True.
      -->
      <SwitchArgument Name="MandatorySwitch" Description="You must enter this switch with a value!" HasValue="True" IsOptional="False"/>
      <SwitchArgument Name="OptionalSwitch" Description="You may add this optional switch"/>
    </ArgumentDefinitions>
  </Main.ArgumentDefinitions>
  
  <!-- Define some variables by setting the variables property. -->
  <Main.Variables>
    <Variables>
      <!-- Uninitialized variable. -->
      <Variable Name="UserInput"/>
      <!-- Variable initialized with boolean value. -->
      <Variable Name="Success" Value="True"/>
      <!-- Variable initialized with number value. -->
      <Variable Name="Number" Value="3"/>
      <!-- Expression macro, the expression will be evaluated every time the expression is used. -->
      <Expression Name="SampleExpression" Value="{NotEqual Left={Get UserInput}, Right={Get Number}}"/>
      <!-- Variable containing an array with three values. -->
      <Variable Name="AnArray">
        <Variable.Value>
          <Array>
            <Variable Name="ItemA" Value="_a_"/>
            <Variable Name="ItemB" Value="_b_"/>
            <Variable Name="ItemC" Value="_c_"/>
          </Array>
        </Variable.Value>
      </Variable>
    </Variables>
  </Main.Variables>

  <!-- Define some functions -->
  <Main.Functions>
    <Functions>
      <!-- Define a sample function with two arguments, 'Argument' and 'AnotherArgument'; AnotherArgument is optional -->
      <Function Name="TestFunction">
        <!-- Output some text -->
        <Echo Text="Inside function TestFunction"/>
        <!-- Output value of Argument. Use the Get expression to access the Argument variable. -->
        <Echo Text="{Get Argument}"/>
        <!-- Test the AnotherArgument value. The variable is optional, if it's missing it will evaluate to false. -->
        <If Condition="{Get AnotherArgument, Optional=True}">
          <!-- Output some text -->
          <Echo Text="AnotherArgument is True. Exit function"/>
          <!-- Return the value 1 -->
          <Return Value="1"/>
        </If>
        <!-- Output the global variable Number -->
        <Echo Text="{Get Number}"/>
      </Function>
    </Functions>
  </Main.Functions>

  <!-- Main program starts here -->

  <Echo Text="Sample usage of Get Set Input and Echo"/>

  <!-- Set the variable 'UserInput'. Use the result of the Input expression as the value -->
  <Set Variable="UserInput" Value="{Input Prompt='Enter some text'}"/>
  <!-- Output some text -->
  <Echo Text="Your input:"/>
  <!-- Output the value of variable UserInput -->
  <Echo Text="{Get UserInput}"/>

  <Echo Text="Sample usage of expression and condition"/>
  <If Condition="{Get SampleExpression}">
    <Echo Text="You did not enter 3"/>
    <If.Else>
      <Echo Text="Thanks for 3"/>
    </If.Else>
  </If>

  <Echo Text="Sample usage of ForEach loop"/>

  <!-- A string is an array of characters, so we can loop over them -->
  <ForEach Variable="loop" In="abcde">
    <!-- loop is the local loop variable -->
    <Echo Text="{Get loop}"/>
  </ForEach>

  <!-- Now loop over the array members of the variable AnArray -->
  <ForEach Variable="item" In="{Get AnArray}">
    <!-- item is the local loop variable -->
    <Echo Text="{Get item}"/>
  </ForEach>

  <Echo Text="Access array member by index"/>
  <Echo Text="{Get AnArray, Index=2}"/>

  <Echo Text="Access array member by member name"/>
  <Echo Text="{Get AnArray, Member=ItemB}"/>

  <Echo Text="Sample usage of Switch statement and binary expressions"/>
  <Switch>
    <!-- The first case where condition evaluates to true will be executed -->
    <Case Condition="{Equal Left='Test', Right={Get UserInput}}">
      <Echo Text="Your input was Test."/>
    </Case>
    <Case>
      <Echo Text="This switch will never be called. Empty condition is always false."/>
    </Case>
    <Case Condition="{GreaterThan Left={Get UserInput}, Right=5}">
      <Echo Text="You typed a number greater than 5."/>
    </Case>
    <!-- If no case matches, default will be called -->
    <Switch.Default>
      <!-- Default expects exactly one statement. Use the Scope statement to add more than one. -->
      <Scope>
        <Echo Text="You typed something else."/>
        <Echo Text=";-("/>
      </Scope>
    </Switch.Default>
  </Switch>

  <Echo Text="Sample usage of Scope and local variables"/>
  <Scope>
    <Scope.Variables>
      <Variables>
        <!-- Use a variable Number, this already exists in Main -->
        <Variable Name="Number" Value="42"/>
      </Variables>
    </Scope.Variables>
    <Echo Text="Output local variable Number"/>
    <Echo Text="{Get Number}"/>
    <Echo Text="Change local variable Number"/>
    <Set Variable="Number" Value="99"/>
    <Echo Text="Output local variable Number"/>
    <Echo Text="{Get Number}"/>
    <Echo Text="Output global variable UserInput inside scope"/>
    <Echo Text="{Get UserInput}"/>
  </Scope>
  <Echo Text="Output global variable Number"/>
  <Echo Text="{Get Number}"/>

  <Echo Text="Sample usage of function calls"/>

  <!-- Call test function with one argument -->
  <Call Function="TestFunction">
    <Argument Name="Argument" Value="-argument1-"/>
  </Call>

  <!-- Call test function with two arguments -->
  <Call Function="TestFunction">
    <Argument Name="AnotherArgument" Value="True"/>
    <Argument Name="Argument" Value="-argument1-"/>
  </Call>

  <!-- Call test function and assing function result to variable number -->
  <Set Variable="Number">
    <Set.Value>
      <Call Function="TestFunction">
        <Argument Name="AnotherArgument" Value="True"/>
        <Argument Name="Argument" Value="-argument1-"/>
      </Call>
    </Set.Value>
  </Set>

  <Echo Text="Output global variable Number after call of function"/>
  <Echo Text="{Get Number}"/>

  <Pause Prompt="Press any key"/>

</Main>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions