Posted by: Zeeshan Amjad | August 1, 2009

Using C# code in XAML


We can also write C# code in XAML and write our complete application in XAML file alone (with embedded C# code in it). All the code is written in CDATA section inside the XAML because XAML parser ignore anything written in CDATA section. But also note that there shouldn’t be any ]] character sequence inside the code, because it will consider end of CDATA section. This is written inside the Code section of XAML. It would be something like this.

  1: <x:Code><![CDATA[
  2: // your C# code here
  3:         ]]>        
  4: </x:Code>
  5: 

Let’s make one working example with XAML inside the C# code. Here is the XAML coding of complete working example of “Time value of money”. This application will calculate the future value of money if enter the present value, interest rate and total number of years.

  1: <Window x:Class="XAMLCode.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="XAML Code Sample" Height="300" Width="400" 
  5:     WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen"
  6:     ResizeMode="NoResize">
  7:     <Grid>
  8:         <Grid.Background>
  9:             <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
 10:                 <GradientStop Color="AliceBlue" Offset="0"></GradientStop>
 11:                 <GradientStop Color="Blue" Offset="1"></GradientStop>
 12:             </LinearGradientBrush>
 13:         </Grid.Background>
 14:         <Grid.RowDefinitions>
 15:             <RowDefinition/>
 16:             <RowDefinition/>
 17:             <RowDefinition/>
 18:             <RowDefinition/>
 19:             <RowDefinition/>
 20:         </Grid.RowDefinitions>
 21:         
 22:         <Grid.ColumnDefinitions>
 23:             <ColumnDefinition/>
 24:             <ColumnDefinition/>
 25:         </Grid.ColumnDefinitions>
 26:         
 27:         <Label Grid.Row="0" Grid.Column="0" Margin="5" VerticalAlignment="Center">
 28:             Present Value
 29:         </Label>
 30:         <TextBox Grid.Row="0" Grid.Column="1" Margin="5" Name="txtPresent">
 31:             
 32:         </TextBox>
 33:         <Label Grid.Row="1" Grid.Column="0" Margin="5" VerticalAlignment="Center">
 34:             Interest Rate    
 35:         </Label>
 36:         <TextBox Grid.Row="1" Grid.Column="1" Margin="5" Name="txtRate">
 37:             
 38:         </TextBox>
 39:         <Label Grid.Row="2" Grid.Column="0" Margin="5" VerticalAlignment="Center">
 40:             No of Years
 41:         </Label>
 42:         <TextBox Grid.Row="2" Grid.Column="1" Margin="5" Name="txtYears">
 43:             
 44:         </TextBox>
 45:         <Label Grid.Row="3" Grid.Column="0" Margin="5" VerticalAlignment="Center">
 46:             Future Value
 47:         </Label>
 48:         <Label Grid.Row="3" Grid.Column="1" Margin="5" VerticalAlignment="Center" Name="lblFuture">
 49:             
 50:         </Label>
 51:         <Button Grid.Row="4" Margin="5,6,5,4" Name="btnExit" Click="OnExit">
 52:             Exit
 53:         </Button>
 54:         <Button Grid.Row="4" Grid.Column="1" Margin="5" Name="btnCalculate" Click="OnCalculate">
 55:             Calculate
 56:         </Button>
 57:     </Grid>
 58:     <x:Code><![CDATA[
 59:         void OnExit(object sender, RoutedEventArgs e)
 60:         {
 61:             Close();
 62:         }
 63:         void OnCalculate(object sender, RoutedEventArgs e)
 64:         {
 65:             double present = Convert.ToDouble(txtPresent.Text);
 66:             double rate = Convert.ToDouble(txtRate.Text) / 100.0;
 67:             double years = Convert.ToDouble(txtYears.Text);
 68:             double future = present * System.Math.Pow(1.0 + rate, years);
 69:             lblFuture.Content = future.ToString("C");
 70:         }
 71:         ]]>        
 72:     </x:Code>
 73: </Window>
 74: 

Here is the output of the program

XAMLCode

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: