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 using System.Threading;

   13 

   14 namespace WEBSWAPP.Silverlight.Samples.ChatAdmin

   15     {

   16     public partial class Page : UserControl

   17         {

   18 

   19         private chatService.ChatSoapClient proxy = new WEBSWAPP.Silverlight.Samples.ChatAdmin.chatService.ChatSoapClient();

   20 

   21         public Page()

   22             {

   23             InitializeComponent();

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

   25 

   26             }

   27 

   28         public void Each_Tick(object o, EventArgs sender)

   29             {

   30             proxy.UsersListAsync();

   31             }

   32 

   33         void Page_Loaded(object sender, RoutedEventArgs e)

   34             {

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

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

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

   38             myDispatcherTimer.Start();

   39 

   40             proxy.PostResponseToUserCompleted += new EventHandler<WEBSWAPP.Silverlight.Samples.ChatAdmin.chatService.PostResponseToUserCompletedEventArgs>(proxy_PostResponseToUserCompleted);

   41             proxy.UsersListCompleted += new EventHandler<WEBSWAPP.Silverlight.Samples.ChatAdmin.chatService.UsersListCompletedEventArgs>(proxy_UsersListCompleted);

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

   43 

   44             }

   45 

   46         void proxy_GetDiscussionByUserNameCompleted(object sender, WEBSWAPP.Silverlight.Samples.ChatAdmin.chatService.GetDiscussionByUserNameCompletedEventArgs e)

   47             {

   48             if (e.Error != null)

   49                 {

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

   51                 tbStatus.Visibility = Visibility.Visible;

   52                 return;

   53                 }

   54             int prevIndex = lbConversation.SelectedIndex;

   55             lbConversation.ItemsSource = null;

   56             lbConversation.ItemsSource = e.Result;

   57             lbConversation.SelectedIndex = prevIndex;

   58             lbConversation.ScrollIntoView(lbConversation.SelectedItem );

   59             }

   60 

   61         void proxy_UsersListCompleted(object sender, WEBSWAPP.Silverlight.Samples.ChatAdmin.chatService.UsersListCompletedEventArgs e)

   62             {

   63             if (e.Error != null)

   64                 {

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

   66                 tbStatus.Visibility = Visibility.Visible;

   67                 return;

   68                 }

   69             int prevSelectedIndex = lbUserNames.SelectedIndex;

   70             lbUserNames.ItemsSource = null;

   71             lbUserNames.ItemsSource = e.Result;

   72             lbUserNames.SelectedIndex = prevSelectedIndex;

   73 

   74             }

   75 

   76         void proxy_PostResponseToUserCompleted(object sender, WEBSWAPP.Silverlight.Samples.ChatAdmin.chatService.PostResponseToUserCompletedEventArgs e)

   77             {

   78             if (e.Error != null)

   79                 {

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

   81                 tbStatus.Visibility = Visibility.Visible;

   82                 return;

   83                 }

   84             lbConversation.ItemsSource = null;

   85             lbConversation.ItemsSource = e.Result;

   86             tbMessage.Text = string.Empty;

   87             tbStatus.Text = string.Empty;

   88             }

   89 

   90         private void Button_Click(object sender, RoutedEventArgs e)

   91             {

   92             if (string.IsNullOrEmpty(tbMessage.Text))

   93                 {

   94                 tbStatus.Text = "You have not entered a response";

   95                 tbStatus.Visibility = Visibility.Visible;

   96                 }

   97             else

   98                 {

   99                 if (lbConversation.SelectedItem != null)

  100                     {

  101                     proxy.PostResponseToUserAsync((lbConversation.SelectedItem as chatService.ChatEntry).UserName,

  102                         (lbConversation.SelectedItem as chatService.ChatEntry).EntryDate, tbMessage.Text);

  103 

  104 

  105                     }

  106                 else

  107                     {

  108                     tbStatus.Text = "Please select an item to respond to it";

  109                     tbStatus.Visibility = Visibility.Visible;

  110                     }

  111 

  112                 }

  113             }

  114 

  115         private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

  116             {

  117             if (e.AddedItems.Count > 0)

  118                 {

  119                 proxy.GetDiscussionByUserNameAsync((e.AddedItems[0] as chatService.ChatUser).UserName);

  120                 }

  121             }

  122 

  123 

  124 

  125         }

  126     }