1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Net;

    5 using System.Windows;

    6 using System.Windows.Controls;

    7 using System.Windows.Documents;

    8 using System.Windows.Input;

    9 using System.Windows.Media;

   10 using System.Windows.Media.Animation;

   11 using System.Windows.Shapes;

   12 

   13 namespace WEBSWAPP.Silverlight.Samples.ChatClient

   14     {

   15     public partial class Page : UserControl

   16         {

   17         private chatService.ChatSoapClient proxy = new WEBSWAPP.Silverlight.Samples.ChatClient.chatService.ChatSoapClient();

   18         public Page()

   19             {

   20             InitializeComponent();

   21             this.Loaded += new RoutedEventHandler(Page_Loaded);

   22 

   23             }

   24         public void Each_Tick(object o, EventArgs sender)

   25             {

   26             if (!string.IsNullOrEmpty(tbUserName.Text))

   27                 proxy.GetDiscussionByUserNameAsync(tbUserName.Text);

   28             }

   29         void Page_Loaded(object sender, RoutedEventArgs e)

   30             {

   31             System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();

   32             myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1 second

   33             myDispatcherTimer.Tick += new EventHandler(Each_Tick);

   34             myDispatcherTimer.Start();

   35 

   36             proxy.SendMessageToCustomerSupportCompleted += new EventHandler<WEBSWAPP.Silverlight.Samples.ChatClient.chatService.SendMessageToCustomerSupportCompletedEventArgs>(proxy_SendMessageToCustomerSupportCompleted);

   37             proxy.GetDiscussionByUserNameCompleted += new EventHandler<WEBSWAPP.Silverlight.Samples.ChatClient.chatService.GetDiscussionByUserNameCompletedEventArgs>(proxy_GetDiscussionByUserNameCompleted);

   38 

   39             }

   40 

   41         void proxy_GetDiscussionByUserNameCompleted(object sender, WEBSWAPP.Silverlight.Samples.ChatClient.chatService.GetDiscussionByUserNameCompletedEventArgs e)

   42             {

   43             if (e.Error != null)

   44                 {

   45                 tbStatus.Text = e.Error.Message + " " + e.Error.InnerException.ToString();

   46                 tbStatus.Visibility = Visibility.Visible;

   47                 return;

   48                 }

   49 

   50             lbConversation.ItemsSource = e.Result;

   51             }

   52 

   53         void proxy_SendMessageToCustomerSupportCompleted(object sender, WEBSWAPP.Silverlight.Samples.ChatClient.chatService.SendMessageToCustomerSupportCompletedEventArgs e)

   54             {

   55             if (e.Error != null)

   56                 {

   57                 tbStatus.Text = e.Error.Message + " " + e.Error.InnerException.ToString();

   58                 tbStatus.Visibility = Visibility.Visible;

   59                 return;

   60                 }

   61 

   62             lbConversation.ItemsSource = e.Result;

   63             tbMessage.Text = string.Empty;

   64             tbStatus.Text = string.Empty;

   65             }

   66 

   67         private void Button_Click(object sender, RoutedEventArgs e)

   68             {

   69             if (string.IsNullOrEmpty(tbUserName.Text))

   70                 {

   71                 tbStatus.Text = "Please enter a user name";

   72                 tbStatus.Visibility = Visibility.Visible;

   73                 }

   74             else

   75                 {

   76                 if(!string.IsNullOrEmpty (tbMessage.Text ))

   77                     proxy.SendMessageToCustomerSupportAsync(tbUserName.Text, tbMessage.Text);

   78                 }

   79             }

   80         }

   81     }