学习移植WPF到Silverlight(2)——HelloSilverlight(移植判断登录是否成功:通过WCF本地数据服务非数据库)

/************************************************************************
* Author: Air灬↓易弦

* Date: 2014.4.20

* License: 如果能对你的学习和项目起到帮助是我的荣幸! 
*
************************************************************************/

 

开发工具:Visual Studio2012 + Microsoft Express Blend4

 

根据上一篇的Demo移植判断登录是否成功:通过WCF本地数据服务非数据库,在WCF服务里面加了一个Admin对象,以此来判断是否登录成功,连接数据库过后就可以从数据库库判断,这儿先简单的测试一下WCF服务。

1.最终效果图

 

最终效果通过WCF服务用本地数据判断是否登录成功。

 

2.添加WCF服务

 

命名为LoginService.svc.

 

添加完会出现LoginService.svc和ILoginService.cs服务接口类

 

3. 创建接口类与WCF服务文件

在HelloSilverlight.Web解决方案下添加Resources/Classes文件夹,在新建一个Admin.cs类文件,里面添加Admin对象代码

/************************************************************************
 * Author:   Air灬↓易弦
 * 
 * Date:     2014.4.20
 * 
 * License:  如果能对你的学习和项目起到帮助是我的荣幸!          
 *
 ************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace HelloSilverlight.Web
{
    [DataContract]
    public class Admin
    {
        public Admin(string adminName, string adminPasswd)
        {
            this.Name = adminName;
            this.Passwd = adminPasswd;
        }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Passwd { get; set; }
    }
}

类名要加[DataContract]修饰,这是WCF数据交互格式,成员用[DataMember],区别于WPF中的类

 

WPF服务建好了,接下来就在Silverlight项目中引用就成

/************************************************************************
 * Author:   Air灬↓易弦
 * 
 * Date:     2014.4.20
 * 
 * License:  如果能对你的学习和项目起到帮助是我的荣幸!          
 *
 ************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace HelloSilverlight.Web
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“LoginService”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 LoginService.svc 或 LoginService.svc.cs,然后开始调试。
    public class LoginService : ILoginService
    {
        public List<Admin> GetAdmin()
        {
            List<Admin> adminList = new List<Admin>();
            Admin admin = new Admin("AirYG", "AirYG");

            adminList.Add(admin);

            return adminList;
        }
}

 

4.在Silverlight中引用WCF服务

在这儿svn服务创建好了后要F5运行一下生成.svn文件,才能在下面的引用

右击Silverlight上面“添加服务引用”就会出现如上图:地址栏里面填写例如:localhost:1791/LoginService.svn,然后再点击“转到”,看服务是否开启成功,命名空间最好和Web里面的名字一致。

 

也可以在浏览器里面测试服务是否开启成功。

 

5.修改Web.config文件

这儿,我上篇随笔里面为了使登陆界面的三个控件分离来好更好的有层次感,但现在为了方便就直接将第3个界面控件直接拷贝过来了,当然也可以沿用三个控件层次,只是在第3个控件上面加入委托执行button事件就行了,这儿为了方便就直接拷贝过来了。

 

Silverlight引用了服务过后还要修改Web里面的Web.config文件,特别注意Binding方式,“binding=“basicHttpBinding”,照着代码复制替换掉Web.config文件,里面文件名改一下

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="HelloSilverlight.Web.BlogBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="HelloSilverlight.Web.BlogBehavior"
          name="HelloSilverlight.Web.LoginService">
        <endpoint address="" binding="basicHttpBinding" contract="HelloSilverlight.Web.ILoginService">
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

现在更新一下LoginService服务引用,然后就在MainPage里面执行数据交互操作了

 

6.在MainPage中编写数据交互函数

/************************************************************************
 * Author:   Air灬↓易弦
 * 
 * Date:     2014.4.20
 * 
 * License:  如果能对你的学习和项目起到帮助是我的荣幸!          
 *
 ************************************************************************/

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 HelloSilverlight.LoginService;
using System.ServiceModel.Channels;
using System.ServiceModel;

namespace HelloSilverlight
{
    public partial class MainPage : UserControl
    {
        private string WCFuri = "http://localhost:1791/LoginService.svc";
        private LoginServiceClient client;
        private List<Admin> adminList;


        public MainPage()
        {
            InitializeComponent();

            adminList = new List<Admin>();

            InitWCFService();
        }

        private void InitWCFService()
        {
            Binding binding = new BasicHttpBinding();
            EndpointAddress endPoint = new EndpointAddress(WCFuri);

            client = new LoginServiceClient(binding, endPoint);
            client = new LoginServiceClient();

            client.GetAdminCompleted += new EventHandler<GetAdminCompletedEventArgs>(client_GetAdminCompleted);
            client.GetAdminAsync();
        }

        private void client_GetAdminCompleted(object sender, GetAdminCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                adminList = e.Result.ToList<Admin>();
            }
        }

        private void btnLogin_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (var admin in adminList)
            {
                if (admin.Name == txtName.Text)
                {
                    if (admin.Passwd == txtPasswd.Text)
                        MessageBox.Show("登陆成功!");
                    else
                        MessageBox.Show("密码错误!");
                }
                else
                {
                    MessageBox.Show("用户名不存在!");
                }
            }
        }
    }
}

虽然在Silverlight的服务配置文件ServiceReferences.ClientConfig里面已经填写了默认的从Web项目的WCF服务中继承了过来,但这儿在NewWCF服务时候还是最好写一下,也可以将起写入配置文件里面,以便以后如果服务器和Silverlight客户端部署到不同的服务器上。到此WCF服务已经在Silverlight上架设好了。点击F5运行,看效果。

 

(1)用户名不存在

 

(2)密码错误

 

(3)登陆成功

 

这儿简单的说一下注册流程,其实推都推得出来,原理和判断登陆一样,就是在LoginService.svn.cs文件里面加段获取注册的函数,再在MainPage.xaml.cs里面加一段注册的函数,就行了,我下面简单的写一下这两个函数。

LoginService.svn.cs文件

/************************************************************************
 * Author:   Air灬↓易弦
 * 
 * Date:     2014.4.20
 * 
 * License:  如果能对你的学习和项目起到帮助是我的荣幸!          
 *
 ************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace HelloSilverlight.Web
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“LoginService”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 LoginService.svc 或 LoginService.svc.cs,然后开始调试。
    public class LoginService : ILoginService
    {
        public List<Admin> GetAdmin()
        {
            List<Admin> adminList = new List<Admin>();
            Admin admin = new Admin("AirYG", "AirYG");

            adminList.Add(admin);

            return adminList;
        }

        public void RegeditAdmin(string adminName, string adminPasswd)
       {
            //写入数据库操作
    
        }
    }

   
}

 

 

MainPage.xaml.cs文件

/************************************************************************
 * Author:   Air灬↓易弦
 * 
 * Date:     2014.4.20
 * 
 * License:  如果能对你的学习和项目起到帮助是我的荣幸!          
 *
 ************************************************************************/

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 HelloSilverlight.LoginService;
using System.ServiceModel.Channels;
using System.ServiceModel;

namespace HelloSilverlight
{
    public partial class MainPage : UserControl
    {
        private string WCFuri = "http://localhost:1791/LoginService.svc";
        private LoginServiceClient client;
        private List<Admin> adminList;


        public MainPage()
        {
            InitializeComponent();

            adminList = new List<Admin>();

            InitWCFService();
        }

        private void InitWCFService()
        {
            Binding binding = new BasicHttpBinding();
            EndpointAddress endPoint = new EndpointAddress(WCFuri);

            client = new LoginServiceClient(binding, endPoint);
            client = new LoginServiceClient();

            client.GetAdminCompleted += new EventHandler<GetAdminCompletedEventArgs>(client_GetAdminCompleted);
            client.GetAdminAsync();
        }

        private void client_GetAdminCompleted(object sender, GetAdminCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                adminList = e.Result.ToList<Admin>();
            }
        }

        private void btnLogin_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (var admin in adminList)
            {
                if (admin.Name == txtName.Text)
                {
                    if (admin.Passwd == txtPasswd.Text)
                        MessageBox.Show("登陆成功!");
                    else
                        MessageBox.Show("密码错误!");
                }
                else
                {
                    MessageBox.Show("用户名不存在!");
                }
            }
        }

        //注册按钮点击注册
        private void btnRegedit_Click_1(object sender, RoutedEventArgs e)
        {
             string adminName = txtName.text;
             string adminPasswd = txtPasswd.text;

             client.RegeditAdminAsync(adminName, adminPasswd);
        }
    }
}

这儿只是简单的写了一下两个文件交互的函数,接下来重点会在数据写入数据中,因为当初我们数据库选用的是MySQL,所以在第3篇随笔中我会用DBLinq链接MySQL然后与Web解决方案中的WCF服务一起完成数据写入操作。

 

7.源码下载Url

http://url.cn/JriyKh

 

文件名:(HelloSilverlight2014.4.20.rar)

posted @ 2014-04-20 23:56  Air灬↓易弦  阅读(487)  评论(0编辑  收藏  举报