How to: Create a Client

 

Applies To: Windows Server 2012 Essentials, Windows Home Server 2011, Windows Storage Server 2008 R2 Essentials, Windows Small Business Server 2011 Essentials

The client application is not required for a provider; however, it is included to represent how the operations of the provider can be used. This client is a simple graphical interface that communicates with the provider through the ChatObjectModel object.

Before you start the procedures in this section, you should complete the procedures listed in How to: Create a Host Process.

To create the client application

  1. Right-click the MyProvider solution, click Add, and then click New Project.

  2. In the Templates pane, click WPF Application, type ChatWindow in the Name text box, and then click OK.

  3. Add a reference to the ChatSample.ObjectModel project.

  4. Add a reference to the WssgCommon.dll file.

  5. Open the MainWindow.xaml file and make sure that it contains the following code.

    <Window x:Class="ChatWindow.MainWindow"  
       xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"  
       xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"  
       xmlns:d="https://schemas.microsoft.com/expression/blend/2008"  
       xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"  
    
       Title="Window1" Height="350" Width="525"   
       xmlns:ChatObjectModel="clr-namespace:ChatSample.ObjectModel;assembly=ChatSample.ObjectModel">  
        <Grid>  
            <TextBox HorizontalAlignment="Left" Margin="81.183,8,0,0" x:Name="UserName"  
             VerticalAlignment="Top" Text="<enter user name>" TextWrapping="Wrap"  
             Width="167"/>  
            <Label HorizontalAlignment="Left" Margin="8,6,0,0" VerticalAlignment="Top"   
             Content="User name:"/>  
            <Button d:LayoutOverrides="Width" HorizontalAlignment="Right" Margin="0,8,8,0"  
             x:Name="ConnectButton" VerticalAlignment="Top" Content="Connect"   
             Click="ConnectButton_Click"/>  
            <ListBox IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Right"   
             Margin="0,47,8,8" x:Name="CurrentUsers" Width="154"/>  
            <TextBox Margin="8,47,166,33.96" Text="" TextWrapping="Wrap" x:Name="ChatText"/>  
            <TextBox d:LayoutOverrides="Height" Margin="8,0,202.507,8"   
             VerticalAlignment="Bottom" Text="<enter chat text>" TextWrapping="Wrap"   
             x:Name="ChatEntry" IsEnabled="False"/>  
            <Button d:LayoutOverrides="Width, Height" HorizontalAlignment="Right"   
             Margin="0,0,166,8" VerticalAlignment="Bottom" Content="Send" x:Name="Send"  
             Click="Send_Click" IsDefault="True" IsEnabled="False"/>  
        </Grid>  
    </Window>  
    
  6. Save the file.

  7. Open the MainWindow.xaml.cs file and make sure that it contains the following code.

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Data;  
    using System.Windows.Documents;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Imaging;  
    using System.Windows.Navigation;  
    using System.Windows.Shapes;  
    using System.Windows.Threading;  
    using System.ComponentModel;  
    using ChatSample.ObjectModel;  
    
    namespace ChatWindow {  
        public partial class MainWindow : Window  
        {  
          public MainWindow() {  
             InitializeComponent();  
          }  
    
          private void ConnectButton_Click(object sender, RoutedEventArgs e) {  
             if (this.DataContext == null)  
                    this.DataContext = new ObjectModel();  
             ObjectModel objModel = (ObjectModel)this.DataContext;  
             objModel.Connect(this.UserName.Text);  
             objModel.ResponseReceived += ChatReceived;  
             objModel.PropertyChanged += objModel_PropertyChanged;  
             this.ConnectButton.IsEnabled = false;  
             this.UserName.IsEnabled = false;  
          }  
    
          void objModel_PropertyChanged(object sender, PropertyChangedEventArgs e) {  
             if (e.PropertyName == "Connected") {  
                this.Dispatcher.Invoke(DispatcherPriority.Normal,  
                   new Action(() => this.ConnectedChanged()));  
             }  
          }  
    
          private void ConnectedChanged() {  
             ObjectModel objModel = this.DataContext as ObjectModel;  
             bool connected = objModel.Connected;  
    
             if (connected) {  
                this.UserName.IsReadOnly = true;  
                this.ChatEntry.IsEnabled = true;  
                this.Send.IsEnabled = true;  
             } else {  
                this.ChatEntry.IsEnabled = false;  
                this.Send.IsEnabled = false;  
                this.ChatText.Text = this.ChatText.Text + "<Disconnected>"  
                   + Environment.NewLine;  
                this.ConnectButton.IsEnabled = true;  
             }  
          }  
    
          private void ChatReceived(object sender, ChatReceivedEventArgs e) {  
             string formattedLine = String.Format("[{0}] {1}{2}",  
                e.User, e.Text, Environment.NewLine);  
             this.ChatText.Text = this.ChatText.Text + formattedLine;  
          }  
    
          private void Send_Click(object sender, RoutedEventArgs e) {  
             ObjectModel objModel = this.DataContext as ObjectModel;  
                objModel.SendChat(this.ChatEntry.Text);  
          }  
       }  
    }  
    
  8. Save the project and build the solution.