Wednesday, January 23, 2013

Windows Phone using web services

for using web services (SOAP)  we need to follow few simple steps. for this purpose i wants to introduce a project with the name CurrencyConversion , add a service reference with the url

http://www.webservicex.net/CurrencyConvertor.asmx

you can see the wsdl at http://www.webservicex.net/CurrencyConvertor.asmx?wsdl



Now just copy the code to yours layout at MainPage.xaml


<phone:PhoneApplicationPage
    x:Class="CurrencyConversion.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot contains the root grid where all other page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
            <TextBlock x:Name="ApplicationTitle" Text="Currency Conversion" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentGrid" Grid.Row="1">
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="24,14,0,0" Name="textBlock1" Text="Amount to Convert" VerticalAlignment="Top" />
            <TextBox Height="68" HorizontalAlignment="Left" Margin="6,36,0,0" Name="txtAmountToConvert" Text="" VerticalAlignment="Top" Width="446" />
            <ListBox Height="93" HorizontalAlignment="Left" Margin="24,137,0,0" Name="lstConvertFrom" VerticalAlignment="Top" Width="220" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="24,101,0,0" Name="textBlock2" Text="Convert from (currency)" VerticalAlignment="Top" Width="220" />
            <TextBlock Height="28" HorizontalAlignment="Left" Margin="262,101,0,0" Name="textBlock3" Text="Convert to (currency)" VerticalAlignment="Top" Width="190" />
            <ListBox Height="93" HorizontalAlignment="Left" Margin="263,137,0,0" Name="lstConvertTo" VerticalAlignment="Top" Width="205" />
            <Button Content="Convert" Height="72" HorizontalAlignment="Left" Margin="24,378,0,0" Name="btnConvert" VerticalAlignment="Top" Width="428" Click="btnConvert_Click" />
         
            <TextBlock Height="33" HorizontalAlignment="Left" Margin="24,258,0,0" Name="txtStatus" Text="status" VerticalAlignment="Top" Width="444" />
            <TextBlock Height="67" HorizontalAlignment="Left" Margin="24,305,0,0" Name="txtTotalConverted" Text="total converted" VerticalAlignment="Top" Width="444" Foreground="#FF004DFF" />
        </Grid>
    </Grid>
   
    <!-- Sample code showing usage of ApplicationBar
    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton x:Name="appbar_button1" IconUri="/Images/appbar_button1.png" Text="Button 1"></shell:ApplicationBarIconButton>
            <shell:ApplicationBarIconButton x:Name="appbar_button2" IconUri="/Images/appbar_button2.png" Text="Button 2"></shell:ApplicationBarIconButton>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem x:Name="menuItem1" Text="MenuItem 1"></shell:ApplicationBarMenuItem>
                <shell:ApplicationBarMenuItem x:Name="menuItem2" Text="MenuItem 2"></shell:ApplicationBarMenuItem>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>
    -->


</phone:PhoneApplicationPage>


the MainPage.xaml.cs code is given here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Reactive;
using System.Threading;
using System.Reflection;

namespace CurrencyConversion
{
    public partial class MainPage : PhoneApplicationPage
    {
        svcCurrencyConverter.CurrencyConvertorSoapClient currencyClient = new svcCurrencyConverter.CurrencyConvertorSoapClient();
        Double dblRate = 0.0;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            LoadCurrencies();

            //create subscription to the web service
            var currency = Observable.FromEvent<svcCurrencyConverter.ConversionRateCompletedEventArgs>(currencyClient, "ConversionRateCompleted");

            currency.ObserveOn(Deployment.Current.Dispatcher).Subscribe(evt =>
            {
                dblRate = evt.EventArgs.Result;
                txtStatus.Text = "The current rate is 1 " + lstConvertFrom.SelectedItem.ToString()  + " to " + evt.EventArgs.Result.ToString() + " " + lstConvertTo.SelectedItem.ToString();
                
                if (txtAmountToConvert.Text.Length>0)
                {
                    //TODO: add error-handling
                    Double decTotal = evt.EventArgs.Result * Convert.ToDouble(txtAmountToConvert.Text);
                    txtTotalConverted.Text = txtAmountToConvert.Text + " " + lstConvertFrom.SelectedItem.ToString() + " = " + decTotal.ToString() + " " + lstConvertTo.SelectedItem.ToString(); 
                }
            },
                ex => { txtStatus.Text = "Sorry, we encountered a problem: " + ex.Message; }
            );            
        }

       

        private void btnConvert_Click(object sender, RoutedEventArgs e)
        {
            currencyClient.ConversionRateAsync((svcCurrencyConverter.Currency) lstConvertFrom.SelectedItem, (svcCurrencyConverter.Currency) lstConvertTo.SelectedItem);
        }

        private void LoadCurrencies()
        {
            //preloading only certain currencies can be accomplished this way
            //lstConvertFrom.Items.Add(svcCurrencyConverter.Currency.USD);
            //lstConvertFrom.Items.Add(svcCurrencyConverter.Currency.EUR);
            //lstConvertFrom.Items.Add(svcCurrencyConverter.Currency.RUB);

            //lstConvertTo.Items.Add(svcCurrencyConverter.Currency.USD);
            //lstConvertTo.Items.Add(svcCurrencyConverter.Currency.EUR);
            //lstConvertTo.Items.Add(svcCurrencyConverter.Currency.RUB);

            //preloading all the currencies
            lstConvertFrom.ItemsSource = GetValues();
            lstConvertTo.ItemsSource = GetValues();
        }

        private object[] GetValues()
        {
            var currencyEnum = typeof(svcCurrencyConverter.Currency);
            List<object> values = new List<object>();

            var fields = from field in currencyEnum.GetFields()
                         where field.IsLiteral
                         select field;

            foreach (FieldInfo field in fields)
            {
                object value = field.GetValue(currencyEnum);
                values.Add(value);
            }

            return values.ToArray();
        }
    }
}


No comments:

Post a Comment