微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight和WebService的简单应用

功能实现显示学生的信息:

 

Student::

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SilverlightApplication3.Web
{
    public class Student
    {
        public string ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public int Age
        {
            get;
            set;
        }

        public string Dep
        {
            get;
            set;
        }
    }
}


 

新建一个Web服务:

 public class StudentService : System.Web.Services.WebService
    {

        [WebMethod]
        public Student[] GetStudent()
        {
            List<Student> students = new List<Student>()
           {
               new Student{ID="1",Name="刘涛",Age=21,Dep="Computer"},new Student{ID="2",Name="张华",Age=22,new Student{ID="3",Name="程勇",new Student{ID="4",Name="李建",Age=23,Dep="Computer"}
           };
            return students.ToArray();
        }
    }


 

 

在silverlight项目中引用上面的服务:

MainPage.xaml

<UserControl x:Class="SilverlightApplication3.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" Loaded="UserControl_Loaded">

    <Grid Background="#46461F">
        <Grid.RowDeFinitions>
            <RowDeFinition Height="40"></RowDeFinition>
            <RowDeFinition Height="*"></RowDeFinition>
        </Grid.RowDeFinitions>
        <Grid.ColumnDeFinitions>
            <ColumnDeFinition></ColumnDeFinition>
        </Grid.ColumnDeFinitions>
        <Border Grid.Row="0" Grid.Column="0" CornerRadius="15"
            Width="240" Height="36" Background="Orange"
            Margin="20 0 0 0" HorizontalAlignment="Left">
            <TextBlock Text="最新随笔" Foreground="White"
                   HorizontalAlignment="Left" VerticalAlignment="Center"
                   Margin="20 0 0 0"></TextBlock>
        </Border>
        <ListBox x:Name="Students" Grid.Row="1" Margin="40 10 10 10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding ID}" Height="40" Foreground="Red"></TextBlock>
                        <TextBlock Text="{Binding Name}" Height="40"></TextBlock>
                        <TextBlock Text="{Binding Age}" Height="40" Foreground="Orange"></TextBlock>
                        <TextBlock Text="{Binding Dep}" Height="40" Foreground="Orange"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>


 

 

MainPage.xaml.cs

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 SilverlightApplication3.StudentService;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender,RoutedEventArgs e)
        {
            StudentServiceSoapClient client = new StudentServiceSoapClient();
            client.GetStudentCompleted += new EventHandler<GetStudentCompletedEventArgs>(client_Completed);
            client.GetStudentAsync();
        }

        private void client_Completed(object sender,GetStudentCompletedEventArgs e)
        {
            if(e.Error==null)
            {
                Students.ItemsSource= e.Result;
            }
        }

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐