Silverlight+WCF实现聊天功能

Silverlight+WCF实现聊天功能,第1张

概述这是来公司做的第二个项目实例。最近在学习WCF方面的知识,看了一些资料,于是做了这个可以实现聊天功能的项目实例。 新建WCF服务和Silverlight项目就不说了主要来看代码: WCF服务契约与数据契约: <span style="color:#FF0000;"> [ServiceContract(CallbackContract = typeof(IMessageCallBack))]</s

这是来公司做的第二个项目实例。最近在学习WCF方面的知识,看了一些资料,于是做了这个可以实现聊天功能的项目实例。

新建WCF服务和Silverlight项目就不说了主要来看代码:

WCF服务契约与数据契约:

 <span > [ServiceContract(CallbackContract = typeof(IMessageCallBack))]</span>public interface IMessageManage    {        [OperationContract]        bool SendMessage(MessageInfo msg);        [OperationContract]        bool RequestMessage(UserInfo user);        [OperationContract]               List<UserInfo> GetUserInfo();        [OperationContract]                bool createuser(UserInfo user);    } public interface IMessageCallBack    {<span >        [OperationContract(IsOneWay = true)]        voID GetMessage(MessageInfo message);</span>    }<pre name="code" ><span >[DataContract]</span>
 public class MessageInfo { [DataMember] public UserInfo FromUser { get; set; } [DataMember] public UserInfo ToUser { get; set; } [DataMember] public string Content { get; set; } [DataMember] public DateTime SendTime { get; set; } }[DataContract] public class UserInfo { [DataMember] public int ID { get; set; } [DataMember] public string name { get; set; } }

 WCF的实现: 

using System;using System.Collections.Generic;using System.linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;<span >using WcfModel;</span>namespace WcfService{    public class MessageService : IMessageManage    {        private static Dictionary<int,IMessageCallBack> DicCallChannel = new Dictionary<int,IMessageCallBack>();        public static List<UserInfo> getuserTemp = new List<UserInfo>();        private int UserID = 0;        public bool createuser(UserInfo user)        {            if (getuserTemp == null)                getuserTemp = new List<UserInfo>();                        bool r = false;            foreach (UserInfo List in getuserTemp)            {                if (List.ID.Equals(user.ID))                {                    r = true;                }            }            if (r)            {                return false;            }            getuserTemp.Add(user);            return true;        }        public bool SendMessage(MessageInfo msg)        {            if (DicCallChannel.Keys.Contains(msg.ToUser.ID))            {                DicCallChannel[msg.ToUser.ID].GetMessage(msg);                return true;            }            return false;        }        public bool RequestMessage(UserInfo user)        {            IMessageCallBack channel = OperationContext.Current.GetCallbackChannel<IMessageCallBack>();            if (!DicCallChannel.Keys.Contains(user.ID))            {                DicCallChannel.Add(user.ID,channel);                UserID = user.ID;                OperationContext.Current.Channel.Closing += Channel_Closing;            }            return true;        }        voID Channel_Closing(object sender,EventArgs e)        {            if (DicCallChannel.Keys.Contains(UserID))            {                DicCallChannel.Remove(UserID);            }        }           public List<UserInfo> GetUserInfo()        {                        return getuserTemp;        }    }}
MainPage前台代码:

<UserControl x:Class="SilverlightApplication.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:d="http://schemas.microsoft.com/Expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d"    d:DesignHeight="600" d:DesignWIDth="800" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">    <ScrollVIEwer HorizontalScrollbarVisibility="auto" VerticalScrollbarVisibility="auto" >        <GrID x:name="LayoutRoot" Background="White" ScrollVIEwer.HorizontalScrollbarVisibility="auto" ScrollVIEwer.VerticalScrollbarVisibility="auto" Height="700">            <StackPanel  OrIEntation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center" ScrollVIEwer.HorizontalScrollbarVisibility="auto" ScrollVIEwer.VerticalScrollbarVisibility="auto" Height="456" margin="286,92,259,152" WIDth="228">                <!--<button x:name="btncreateuser1" Content="Create User1" WIDth="100" margin="0,10,0" Click="btncreateuser1_Click" />            <button x:name="btncreateuser2" Content="Create User2" WIDth="100" margin="0,0" Click="btncreateuser2_Click" />-->                <telerik:GroupBox Height="300" name="groupBox2" WIDth="200" header="收">                    <TextBox x:name="txtContentRecord" WIDth="182" Height="261" IsReadonly="True" margin="0,0" textwrapPing="Wrap" />                </telerik:GroupBox>                <telerik:GroupBox Height="107" name="groupBox3" WIDth="200" header="发" margin="0,30,0">                    <StackPanel>                        <TextBox x:name="txtContent" WIDth="182" Height="30" margin="0,0"  AllowDrop="False" AcceptsReturn="True" textwrapPing="Wrap"   />                        <button x:name="btnSend" Content="Send" WIDth="100" margin="0,0" Click="btnSend_Click">                        </button>                    </StackPanel>                </telerik:GroupBox>            </StackPanel>            <StackPanel margin="27,514,152">                <!--<ListBox WIDth="100" Height="60" x:name="lstgu" ></ListBox>-->                <!--<TextBox x:name="txtGU" WIDth="100" Height="100"></TextBox>-->                <StackPanel OrIEntation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" margin="0,0" WIDth="202" Height="209">                    <telerik:GroupBox Height="180" name="groupBox4" WIDth="118" header="User List" margin="10,0">                        <sdk:DataGrID DataContext="{Binding}" autoGenerateColumns="False" Height="125" name="dataGrID1" WIDth="89" margin="0,0" SelectionChanged="dataGrID1_SelectionChanged" IsReadonly="True">                            <sdk:DataGrID.Columns>                                <sdk:DataGrIDTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" header="ID" WIDth="auto" Binding="{Binding ID}" />                                <sdk:DataGrIDTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" header="name" WIDth="auto" Binding="{Binding name}"  />                            </sdk:DataGrID.Columns>                        </sdk:DataGrID>                    </telerik:GroupBox>                    <button x:name="btnRefreshUser" Click="btnRefreshUser_Click"  WIDth="63" Height="20" margin="10,0" IsEnabled="False">Refresh</button>                </StackPanel>                <telerik:GroupBox   Height="116" name="groupBox1" WIDth="202" header="User Login">                    <StackPanel>                        <StackPanel OrIEntation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" margin="0,0">                            <TextBlock TextAlignment="Center" VerticalAlignment="Center">ID:</TextBlock>                            <TextBox x:name="CurrentID" WIDth="60" margin="21,0" ></TextBox>                        </StackPanel>                        <StackPanel  OrIEntation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">                            <TextBlock TextAlignment="Center" VerticalAlignment="Center">name:</TextBlock>                            <TextBox x:name="Currentname" WIDth="60" ></TextBox>                        </StackPanel>                        <StackPanel x:name="login">                            <button x:name="Login" Click="Login_Click" WIDth="95" margin="0,0">Login</button>                        </StackPanel>                    </StackPanel>                </telerik:GroupBox>                <!--<telerik:GroupBox Height="134" name="groupBox5" WIDth="202" header="Create User">                    <StackPanel>                        <StackPanel OrIEntation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" margin="0,0">                            <TextBlock TextAlignment="Center" VerticalAlignment="Center">ID:</TextBlock>                            <TextBox x:name="createuserID" WIDth="60" margin="21,0"></TextBox>                        </StackPanel>                        <StackPanel OrIEntation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">                            <TextBlock TextAlignment="Center" VerticalAlignment="Center">name:</TextBlock>                            <TextBox x:name="createusername" WIDth="60"></TextBox>                        </StackPanel>                        <StackPanel OrIEntation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" margin="0,0">                            <button x:name="btncreateuser" Click="btncreateuser_Click">createuser</button>                        </StackPanel>                    </StackPanel>                </telerik:GroupBox>-->            </StackPanel>        </GrID>    </ScrollVIEwer></UserControl>
MainPage后台代码:

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 SilverlightApplication.MessageService;using System.ComponentModel;using System.Collections.ObjectModel;namespace SilverlightApplication{    public partial class MainPage : UserControl    {        MessageInfo message = new MessageInfo();        public  List<UserInfo> Listuser = new List<UserInfo>();        //string p;        //string p_2;        //string p_3;        //bool refresh = false;        public MainPage()        {            InitializeComponent();            getListuser();            //UserInfo rtouser = new UserInfo();            //rtouser.ID=Convert.ToInt32(this.CurrentID.Text);            //rtouser.name = this.Currentname.Text;        }        //public MainPage(string p,string p_2,string p_3)        //{        //    InitializeComponent();        //    this.p = p;        //    this.p_2 = p_2;        //    this.CurrentID.Text = p;        //    this.Currentname.Text = p_2;        //    this.txtContentRecord.Text = p_3;        //    rgetListuser();        //    this.CurrentID.IsReadonly = true;        //    this.Currentname.IsReadonly = true;        //    this.login.Visibility = Visibility.Collapsed;        //}                public voID getListuser()        {            MessageManageClIEnt clIEnt = new MessageManageClIEnt();            clIEnt.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(clIEnt_GetUserInfoCompleted);            clIEnt.GetUserInfoAsync();        }        voID clIEnt_GetUserInfoCompleted(object sender,GetUserInfoCompletedEventArgs e)        {            if (e.Result != null)            {                foreach (var List in e.Result)                {                    Listuser.Add(List);                }                //this.dataGrID1.ItemsSource = Listuser;            }            else            {                MessageBox.Show("当前用户列表为空!");            }        }        //public voID rgetListuser()        //{        //    ChatServiceClIEnt clIEnt = new ChatServiceClIEnt();        //    clIEnt.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(clIEnt_GetUserInfoCompletedR);        //    clIEnt.GetUserInfoAsync();        //}        //voID clIEnt_GetUserInfoCompletedR(object sender,GetUserInfoCompletedEventArgs e)        //{        //    Listuser.Clear();        //    if (e.Result != null)        //    {        //        foreach (var List in e.Result)        //        {        //            Listuser.Add(List);        //        }        //        this.dataGrID1.ItemsSource = Listuser;        //    }        //    else        //    {        //        MessageBox.Show("当前用户列表为空!");        //    }        //}        //private ObservableCollection<UserInfo> _Listuser = null;        //private voID btncreateuser1_Click(object sender,RoutedEventArgs e)        //{        //    this.btncreateuser2.IsEnabled = false;        //    ChatServiceClIEnt clIEnt = new ChatServiceClIEnt();        //    UserInfo formUser = new UserInfo()        //    {        //        ID = 123,//        name = "张三"        //    };        //    //creatuser1 = formUser;        //    //getuser.Add(formUser);        //    UserInfo toUser = new UserInfo()        //    {        //        ID = 234,//        name = "李四",//    };        //    message.FromUser = formUser;        //    message.ToUser = toUser;        //    message.Content = txtContent.Text;        //    message.SendTime = DateTime.Now;        //    clIEnt.RequestMessageCompleted += new EventHandler<RequestMessageCompletedEventArgs>(clIEnt_RequestMessageCompleted);        //    clIEnt.RequestMessageAsync(formUser);        //    //clIEnt.GetUserInfoAsync(formUser);        //    clIEnt.GetMessageReceived += new EventHandler<GetMessageReceivedEventArgs>(clIEnt_GetMessageReceived);        //    //clIEnt.RequestMessageCompleted += (rs,re) =>        //    //{        //    //    if (re.Result)        //    //    {        //    //        MessageBox.Show("用户" + formUser.ID + "创建成功!");        //    //    }        //    //};        //    //clIEnt.GetMessageReceived += (gs,ge) =>        //    //{        //    //    if (ge.Error == null)        //    //    {        //    //        txtContentRecord.Text += "\r" + "来自:" + ge.message.FromUser.name + ",内容:" + ge.message.Content + ",时间:" + ge.message.SendTime.ToShortTimeString();        //    //    }        //    //};        //}        //voID clIEnt_RequestMessageCompleted(object sender,RequestMessageCompletedEventArgs e)        //{        //    if (e.Result)        //    {        //        MessageBox.Show("通道创建成功!");        //    }        //}        //voID clIEnt_GetMessageReceived(object sender,GetMessageReceivedEventArgs e)        //{        //   if (e.Error == null)        //        {        //            //txtContentRecord.Text +="\r\n"+ "来自:" + e.message.FromUser.name + ",内容:" + e.message.Content + ",时间:" + e.message.SendTime.ToShortTimeString()+"\r\n";        //            if (this.txtContentRecord.Text == "")        //            {        //                txtContentRecord.Text +=e.message.FromUser.name + e.message.SendTime.ToShortTimeString() + ":"+"\r\n" + e.message.Content ;        //            }        //            else         //            {        //                txtContentRecord.Text += "\r\n" + e.message.FromUser.name + e.message.SendTime.ToShortTimeString() + ":" + "\r\n" + e.message.Content;        //            }        //        }        //}        //private voID btncreateuser2_Click(object sender,RoutedEventArgs e)        //{        //    this.btncreateuser1.IsEnabled = false;        //    ChatServiceClIEnt clIEnt = new ChatServiceClIEnt();        //    UserInfo formUser = new UserInfo()        //    {        //        ID = 234,//        name = "李四"        //    };        //    //creatuser2 = formUser;        //    //getuser.Add(formUser);        //    UserInfo toUser = new UserInfo()        //    {        //        ID = 123,//        name = "张三",//    };        //    message.FromUser = formUser;        //    message.ToUser = toUser;        //    message.Content = txtContent.Text;        //    message.SendTime = DateTime.Now;        //    clIEnt.RequestMessageCompleted += new EventHandler<RequestMessageCompletedEventArgs>(clIEnt_RequestMessageCompleted1);        //    clIEnt.RequestMessageAsync(formUser);        //    clIEnt.GetMessageReceived += new EventHandler<GetMessageReceivedEventArgs>(clIEnt_GetMessageReceived1);        //    //clIEnt.GetUserInfoAsync(formUser);        //    //clIEnt.RequestMessageCompleted += (rs,时间:" + ge.message.SendTime.ToShortTimeString();        //    //    }        //    //};        //}        private voID btnSend_Click(object sender,RoutedEventArgs e)        {            if (this.txtContent.Text == "" || this.dataGrID1.SelectedItem == null)            {                MessageBox.Show("消息内容为空或没有选择聊天对象!");            }            else            {                MessageManageClIEnt clIEnt = new MessageManageClIEnt();                message.Content = txtContent.Text;                message.SendTime = DateTime.Now;                clIEnt.SendMessageCompleted += new EventHandler<SendMessageCompletedEventArgs>(clIEnt_SendMessageCompleted);                clIEnt.SendMessageAsync(message);                //clIEnt.SendMessageCompleted += (ds,de) =>                //{                //    if (de.Result)                //    {                //        MessageBox.Show("发送成功!");                //    }                //    else                //    {                //        MessageBox.Show("发送失败!");                //    }                //};            }        }        voID clIEnt_SendMessageCompleted(object sender,SendMessageCompletedEventArgs e)        {            if (e.Result)            {                if (this.txtContentRecord.Text == "")                {                    this.txtContentRecord.Text += this.Currentname.Text + DateTime.Now.ToShortTimeString() + ":" + " \r\n" + this.txtContent.Text;                    this.txtContent.Text = "";                    MessageBox.Show("发送成功!");                }                else                {                    this.txtContentRecord.Text += "\r\n" + this.Currentname.Text + DateTime.Now.ToShortTimeString() + ":" + " \r\n" + this.txtContent.Text;                    this.txtContent.Text = "";                    MessageBox.Show("发送成功!");                }            }            else            {                MessageBox.Show("发送失败!");                this.txtContent.Text = "";            }        }        //private voID btnGU_Click(object sender,RoutedEventArgs e)        //{        //    ChatServiceClIEnt clIEnt = new ChatServiceClIEnt();        //    clIEnt.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(clIEnt_GetUserInfoCompleted);        //    clIEnt.GetUserInfoAsync();        //    int i;        //    for (i = 0; i < getuser.Count; i++)        //    {        //        clIEnt.GetUserInfoAsync(getuser[i]);        //    }        //}        //private voID btnGU_Click(object sender,RoutedEventArgs e)        //{        //    MessageManageClIEnt clIEnt = new MessageManageClIEnt();        //    clIEnt.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(clIEnt_GetUserInfoCompleted);        //    clIEnt.GetUserInfoAsync();        //}        //voID clIEnt_GetUserInfoCompleted(object sender,GetUserInfoCompletedEventArgs e)        //{        //    //this.lstgu.ItemsSource = e.Result;        //    this.dataGrID1.ItemsSource = e.Result;        //    //List<UserInfo> List=new List<UserInfo>();        //    //List = e.Result;        //    //this.lstgu.Items.Add(        //    //    new TextBlock        //    //{        //    //    Text = "用户名:",//    //    textwrapPing = textwrapPing.Wrap        //    //}        //    //);        //UserInfo abc=new UserInfo();        //abc = e.Result;        //if (this.txtGU.Text == "")        //{        //    this.txtGU.Text += "ID:" + e.Result.ID + "\r\n" + "name:" + e.Result.name;        //}        //else        //{        //    this.txtGU.Text += "\r\n" + "ID:" + e.Result.ID + "\r\n" + "name:" + e.Result.name;        //}        //}        //private voID btncreateuser_Click(object sender,RoutedEventArgs e)        //{        //    UserInfo create = new UserInfo();        //    create.ID = Convert.ToInt32(this.createuserID.Text);        //    create.name = this.createusername.Text;        //    ChatServiceClIEnt clIEnt = new ChatServiceClIEnt();        //    clIEnt.createuserCompleted += new EventHandler<createuserCompletedEventArgs>(clIEnt_createuserCompleted);        //    clIEnt.createuserAsync(create);        //    clIEnt.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(clIEnt_GetUserInfoCompleted);        //    clIEnt.GetUserInfoAsync();        //    App app = (App)Application.Current;        //    app.RedirectTo(new MainPage());        //}        //voID clIEnt_createuserCompleted(object sender,createuserCompletedEventArgs e)        //{        //    if (e.Result!=null)        //    {        //        MessageBox.Show("用户创建成功!");        //        //_Listuser = e.Result;        //        //this.dataGrID1.ItemsSource = e.Result;        //        this.createuserID.Text = "";        //        this.createusername.Text = "";        //        this.groupBox1.Visibility = Visibility.Visible;        //        this.CurrentID.Text = Convert.ToString(e.Result.ID);        //        this.Currentname.Text = e.Result.name;        //        this.groupBox5.Visibility = Visibility.Collapsed;        //        //lstgu.Items.Add        //        //    (        //        //     new TextBlock        //        //     {        //        //         Text = "ID:" + e.Result.ID+"\n"+ "name:" + e.Result.name,//        //         textwrapPing = textwrapPing.Wrap        //        //     }        //        //    );        //    }        //    else         //    {        //        MessageBox.Show("用户创建失败!");        //    }        //}        //private voID btnGU_Click(object sender,RoutedEventArgs e)        //{        //    ChatServiceClIEnt clIEnt = new ChatServiceClIEnt();        //    clIEnt.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(clIEnt_GetUserInfoCompleted);        //    clIEnt.GetUserInfoAsync();        //}        private voID dataGrID1_SelectionChanged(object sender,SelectionChangedEventArgs e)        {            UserInfo stouser = new UserInfo();            stouser = (UserInfo)this.dataGrID1.SelectedItem;            if (this.dataGrID1.SelectedItem == null)            {                tongxin();            }            else            {                MessageManageClIEnt clIEnt = new MessageManageClIEnt();                UserInfo formUser = new UserInfo()                {                    ID = Convert.ToInt32(this.CurrentID.Text),name = this.Currentname.Text,};                UserInfo toUser = new UserInfo()                {                    ID = stouser.ID,name = stouser.name,};                message.FromUser = formUser;                message.ToUser = toUser;                message.Content = txtContent.Text;                message.SendTime = DateTime.Now;                clIEnt.RequestMessageCompleted += new EventHandler<RequestMessageCompletedEventArgs>(clIEnt_RequestMessageCompleted);                clIEnt.RequestMessageAsync(formUser);                clIEnt.GetMessageReceived += new EventHandler<GetMessageReceivedEventArgs>(clIEnt_GetMessageReceived);            }        }        voID clIEnt_RequestMessageCompleted(object sender,RequestMessageCompletedEventArgs e)        {            if (e.Result)            {                MessageBox.Show("当前用户通信通道创建成功!");            }        }        voID clIEnt_GetMessageReceived(object sender,GetMessageReceivedEventArgs e)        {            if (e.Error == null)            {                //txtContentRecord.Text += "\r\n"+"来自:" + e.message.FromUser.name + ",时间:" + e.message.SendTime.ToShortTimeString()+"\r\n";                if (this.txtContentRecord.Text == "")                {                    txtContentRecord.Text += e.message.FromUser.name + e.message.SendTime.ToShortTimeString() + ":" + "\r\n" + e.message.Content;                }                else                {                    txtContentRecord.Text += "\r\n" + e.message.FromUser.name + e.message.SendTime.ToShortTimeString() + ":" + "\r\n" + e.message.Content;                }            }        }        private voID Login_Click(object sender,RoutedEventArgs e)        {            if (this.CurrentID.Text == "" || this.Currentname.Text == "")            {                MessageBox.Show("ID或name不能为空!");            }            else            {                UserInfo currentuser = new UserInfo();                currentuser.ID = Convert.ToInt32(this.CurrentID.Text);                currentuser.name = this.Currentname.Text;                bool isSuccess = false;                foreach (UserInfo u in Listuser)                {                    if (u.ID.Equals(currentuser.ID) && u.name.Equals(currentuser.name))                    {                        isSuccess = true;                    }                }                if (isSuccess)                {                    MessageBox.Show("用户" + currentuser.name + "登录成功!");                    this.btnRefreshUser.IsEnabled = true;                    this.dataGrID1.ItemsSource = Listuser;                    this.CurrentID.IsReadonly = true;                    this.Currentname.IsReadonly = true;                    this.login.Visibility = Visibility.Collapsed;                    tongxin();                }                else                {                    MessageBox.Show("用户" + currentuser.name + "登录失败!");                }            }        }        private voID btnRefreshUser_Click(object sender,RoutedEventArgs e)        {            //App app = (App)Application.Current;            //app.RedirectTo(new MainPage(this.CurrentID.Text.Trim(),this.Currentname.Text.Trim(),this.txtContentRecord.Text.Trim()));            //this.dataGrID1.ItemsSource = null;            //refresh = true;            MessageManageClIEnt clIEnt = new MessageManageClIEnt();            clIEnt.GetUserInfoCompleted += new EventHandler<GetUserInfoCompletedEventArgs>(clIEnt_GetUserInfoCompleted12);            clIEnt.GetUserInfoAsync();                    }        voID clIEnt_GetUserInfoCompleted12(object sender,GetUserInfoCompletedEventArgs e)        {                        if (e.Result != null)            {                List<UserInfo> rListuser = new List<UserInfo>();                                foreach (var List in e.Result)                {                    rListuser.Add(List);                }                this.dataGrID1.ItemsSource = rListuser;            }            else            {                MessageBox.Show("当前用户列表为空!");            }        }        public voID tongxin()        {            MessageManageClIEnt clIEnt = new MessageManageClIEnt();            UserInfo formUser = new UserInfo()            {                ID = Convert.ToInt32(this.CurrentID.Text),};            UserInfo toUser = new UserInfo()            {                ID = formUser.ID,name = formUser.name,};            message.FromUser = formUser;            message.ToUser = toUser;            message.Content = txtContent.Text;            message.SendTime = DateTime.Now;            clIEnt.RequestMessageCompleted += new EventHandler<RequestMessageCompletedEventArgs>(clIEnt_RequestMessageCompleted123);            clIEnt.RequestMessageAsync(formUser);            clIEnt.GetMessageReceived += new EventHandler<GetMessageReceivedEventArgs>(clIEnt_GetMessageReceived);        }        voID clIEnt_RequestMessageCompleted123(object sender,RequestMessageCompletedEventArgs e)        {            if (e.Result)            {                //MessageBox.Show("当前用户通信通道创建成功!");            }        }    }}

本实例的一些配置主要参照了下文:

http://blog.csdn.net/cyb331/article/details/9185801

感谢该博主。

总结

以上是内存溢出为你收集整理的Silverlight+WCF实现聊天功能全部内容,希望文章能够帮你解决Silverlight+WCF实现聊天功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1011705.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存