天天看点

Silverlight和服务器端通信

Silverlight和服务器端通信

与服务器端通信,首先我们用wcf,新建一个wcf的页面。

添加一个简单的求和的方法:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SLDome.Web

{

    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ISLS”。

    [ServiceContract]

    public interface ISLS

    {

        [OperationContract]

        int Add(int number1, int number2);

    }

}

=============================================================================

然后我们再实现这个接口:

Silverlight和服务器端通信

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SLDome.Web

{

    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“SLS”。

    // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 SLS.svc 或 SLS.svc.cs,然后开始调试。

    public class SLS : ISLS

    {

        public int Add(int number1, int number2)

        {

            return number1 + number2;

        }

    }

}

=================================================================================

现在我们去设置下前台页面:

Silverlight和服务器端通信

<UserControl x:Class="SLDome.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="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

        <TextBox HorizontalAlignment="Left" Height="23" Margin="118,58,0,0" TextWrapping="Wrap" Name="txtName" VerticalAlignment="Top" Width="120"/>

        <TextBox HorizontalAlignment="Left" Height="23" Margin="118,113,0,0" TextWrapping="Wrap" Name="txtPwd" VerticalAlignment="Top" Width="120"/>

        <Button Content="WCF"  Name="btnWcf" HorizontalAlignment="Left" Margin="46,216,0,0" VerticalAlignment="Top" Width="75" Click="btnWcf_Click"/>

        <Button Content="一般处理程序" Name="btnHandler" HorizontalAlignment="Left" Margin="211,216,0,0" VerticalAlignment="Top" Width="110" Click="btnHandler_Click"/>

    </Grid>

</UserControl>

这样我们的wcf就写好了,下面是我们的一般处理程序的代码:

我们新建一个一般处理程序的页面,用来处理用户的登陆:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace SLDome.Web

{

    /// <summary>

    /// LoginHandler 的摘要说明

    /// </summary>

    public class LoginHandler : IHttpHandler

    {

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            string name = context.Request["txtName"].ToString();

            string password = context.Request["txtPwd"].ToString();

            if (name=="admin")

            {

                if (password=="admin123")

                {

                    context.Response.Write("登陆成功!");

                }

                else

                {

                    context.Response.Write("密码错误!");

                }

            }

            else

            {

                context.Response.Write("用户名不存在!");

            }

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

================================================================================

然后我们处理下这个一般处理程序:

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;

namespace SLDome

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

        }

        private void btnWcf_Click(object sender, RoutedEventArgs e)

        {

            ServiceReference1.SLSClient sc = new ServiceReference1.SLSClient();

            sc.AddAsync(10, 20);

            sc.AddCompleted += sc_AddCompleted;

        }

        void sc_AddCompleted(object sender, ServiceReference1.AddCompletedEventArgs e)

        {

            MessageBox.Show(e.Result.ToString());

        }

        private void btnHandler_Click(object sender, RoutedEventArgs e)

        {

            string name = txtName.Text.Trim();

            string password = txtPwd.Text.Trim();

            WebClient wc = new WebClient();

            wc.DownloadStringAsync(new Uri("http://localhost:7090/LoginHandler.ashx?txtName=" + name + "&txtPwd=" + password, UriKind.RelativeOrAbsolute));

            wc.DownloadStringCompleted += wc_DownloadStringCompleted;

        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

        {

            MessageBox.Show(e.Result);

        }

    }

}

原文链接: http://blog.csdn.net/mypc2010/article/details/8073536

版权声明:本文为CSDN博主「weixin_33816611」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_33816611/article/details/92555225