b

Sunday, 20 January 2013

Consuming the Live Currency Conversion Web Service in WPF C#

Consuming the Live Currency Conversion Web Service in WPF

currency-converter-in-wpf-silverlight-xaml-using-C#-VB.net
Step 1)  Add Currency/Exchange convertor WebService Service reference into WPF Application.

Here I choose   http://www.webservicex.net/CurrencyConvertor.asmx?WSDL   


Step)Add 2 combo boxes and 1 button control and one TextBlock to XAML(as shown below)

        <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top"
                  Width="120" Grid.Row="0" x:Name="cbxfrom" Tag="from"
                   Foreground="Goldenrod" Background="gray" FontFamily="Snap ITC" FontSize="16"
                  />
        <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top"
                  Width="120" Grid.Row="0" Margin="136,0,0,0"
                  x:Name="cbxto" Tag="to"
                  Foreground="Goldenrod" Background="gray" FontFamily="Snap ITC" FontSize="16"
                  />
        <Button Content="Convert" HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Width="153" Grid.Row="0" Margin="42,28,0,0"
                Click="Button_Click_1"
                 Foreground="Navy" Background="BlanchedAlmond" FontFamily="Arial Black" FontSize="20"  FontStyle="Italic"
                />
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock"
                   VerticalAlignment="Top"
                   Grid.Row="1" Margin="81,85,0,0" Width="155"
                   FontFamily="Comic Sans MS" FontSize="25" Foreground="white" Background="teal"
                   x:Name="txtConvValue"
                   />
 

Step 3) In the Convert Button Event handler

Call web Service Method



private void Button_Click_1(object sender, RoutedEventArgs e)
{
String from=cbxfrom.SelectedValue as String;

String to=cbxto.SelectedValue as String;
double convValue= client.ConversionRate(

(CurService.Currency)Enum.Parse(typeof(CurService.Currency), from),
(CurService.Currency)Enum.Parse(typeof(CurService.Currency), to));

txtConvValue.Text = convValue.ToString();
}


Step 4) Here Currency converter Namespace is CurService
CurService.CurrencyConvertorSoapClient client = new CurrencyConvertorSoapClient();

Step 5) Load Currency Codes to Combobox


void LoadCurrencyCodes()
{
try
{
String[] CurCodes = Enum.GetNames(typeof(CurService.Currency));

cbxto.ItemsSource = CurCodes;
cbxfrom.ItemsSource = CurCodes;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Here is the Output


 

No comments:

Post a Comment