参考别人的例子,加入跨域访问部分,留作参考.
WebServices 项目: Service1 类和 Person类 注意因为是独立的Service项目,所以需要加入2个xml文件,实现跨域访问,加在根目录下
clientaccesspolicy.xml和crossdomain.xml
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public Person[] GetPeople()
{
List<Person> People = new List<Person>()
{
new Person{ Name="Jack",Age=12},
new Person{ Name="Tom",Age=22},
new Person{ Name="Simon",Age=32},
new Person{ Name="Richard",Age=26}
};
return People.ToArray();
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public Person[] GetPeople()
{
List<Person> People = new List<Person>()
{
new Person{ Name="Jack",Age=12},
new Person{ Name="Tom",Age=22},
new Person{ Name="Simon",Age=32},
new Person{ Name="Richard",Age=26}
};
return People.ToArray();
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
crossdomain.xml
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
服务端项目: 添加WebService的引用
Xaml
<UserControl x:Class="SLWenServices.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">
<StackPanel Width="400" Height="300" Background="Wheat">
<TextBlock Text="通过WebService取得的数据如下" TextAlignment="Center" Foreground="Red" FontSize="18"></TextBlock>
<Button x:Name="btnGetWebService" Width="200" Height="30" Content="获取数据" Click="btnGetWebService_Click"></Button>
<ListBox x:Name="People" Width="300" Height="200" Margin="20">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="姓名" Width="100" Foreground="Blue" ></TextBlock>
<TextBlock Text="年龄" Width="100" Foreground="DarkBlue"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Foreground="Red" Width="100" ></TextBlock>
<TextBlock Text="{Binding Age}" Foreground="Green" Width="100" ></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</UserControl>
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">
<StackPanel Width="400" Height="300" Background="Wheat">
<TextBlock Text="通过WebService取得的数据如下" TextAlignment="Center" Foreground="Red" FontSize="18"></TextBlock>
<Button x:Name="btnGetWebService" Width="200" Height="30" Content="获取数据" Click="btnGetWebService_Click"></Button>
<ListBox x:Name="People" Width="300" Height="200" Margin="20">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="姓名" Width="100" Foreground="Blue" ></TextBlock>
<TextBlock Text="年龄" Width="100" Foreground="DarkBlue"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Foreground="Red" Width="100" ></TextBlock>
<TextBlock Text="{Binding Age}" Foreground="Green" Width="100" ></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</UserControl>
C#
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 SLWenServices.MyWebServiceRef;
namespace SLWenServices
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void btnGetWebService_Click(object sender, RoutedEventArgs e)
{
//使用WebService从服务器端得到数据并在本地端进行处理
Service1SoapClient client = new Service1SoapClient();
client.GetPeopleCompleted += new EventHandler<GetPeopleCompletedEventArgs>(client_GetPeopleCompleted);
client.GetPeopleAsync();
}
void client_GetPeopleCompleted(object sender, GetPeopleCompletedEventArgs e)
{
if (e.Error == null)
{
People.ItemsSource = e.Result; //绑定结果到UI的List控件
}
}
}
}
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 SLWenServices.MyWebServiceRef;
namespace SLWenServices
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void btnGetWebService_Click(object sender, RoutedEventArgs e)
{
//使用WebService从服务器端得到数据并在本地端进行处理
Service1SoapClient client = new Service1SoapClient();
client.GetPeopleCompleted += new EventHandler<GetPeopleCompletedEventArgs>(client_GetPeopleCompleted);
client.GetPeopleAsync();
}
void client_GetPeopleCompleted(object sender, GetPeopleCompletedEventArgs e)
{
if (e.Error == null)
{
People.ItemsSource = e.Result; //绑定结果到UI的List控件
}
}
}
}
Web项目 : 保持默认就好了