Currency Converter in Windows Phone

Here is an example of simple currency converter which will make you understand the concepts like DataBinding and Conversions in windows phone application development. So let’s look at the example to get grip over the two concepts.
First create a simple windows application project and on the main page of your project add this XAML code:

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CurrencyConverter"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <local:dollar x:Key="cmt"/>
        <local:Euro x:Key="e"/>
        <local:Pound x:Key="p"/>
    </phone:PhoneApplicationPage.Resources>
  
    <Grid>
        <StackPanel>
            <TextBlock FontSize="30">Enter Amount:</TextBlock>
            <TextBox Name="txtBox"/>
            <TextBlock>Amount in Dollar</TextBlock>
            <TextBox Text="{Binding ElementName=txtBox, Path=Text,Mode=TwoWay, Converter={StaticResource cmt}}"></TextBox>
            <TextBlock>Amount in Euro</TextBlock>
            <TextBox Text="{Binding ElementName=txtBox, Path=Text,Mode=TwoWay, Converter={StaticResource e}}"></TextBox>
            <TextBlock>Amount in Pound</TextBlock>
            <TextBox Text="{Binding ElementName=txtBox, Path=Text,Mode=TwoWay, Converter={StaticResource p}}"></TextBox>
           
        </StackPanel>
    </Grid>
</phone:PhoneApplicationPage>


The designer view for this project is as shown like:


Now add a Class and name it CurrencyConverter and add this code within this class:

namespace CurrencyConverter
{
    public class dollar : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float i = 0;
            string length = (string)value;
            float.TryParse(length, out i);
            string dolr = (i / 100).ToString();
            return dolr;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float i = 0;
            var length = (string)value;
            float.TryParse(length, out i);
            string dolr = (i * 100).ToString();
            return dolr;
        }

    }

    public class Euro : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float i = 0;
            string length = (string)value;
            float.TryParse(length, out i);
            string dolr = (i / 117).ToString();
            return dolr;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            float i = 0;
            var length = (string)value;
            float.TryParse(length, out i);
            string dolr = (i * 117).ToString();
            return dolr;
        }


    }
    public class Pound : IValueConverter
        {

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                float i = 0;
                string length = (string)value;
                float.TryParse(length, out i);
                string dolr = (i / 153).ToString();
                return dolr;
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                float i = 0;
                var length = (string)value;
                float.TryParse(length, out i);
                string dolr = (i * 153).ToString();
                return dolr;
            }

        }
    }

Now you can check the functionality of the currency converter application.

Currency Converter in Windows Phone Currency Converter in Windows Phone Reviewed by Afsar Murad on 02:51 Rating: 5
Powered by Blogger.