我有一个组合框,其中包含所有品牌的列表,我需要将这个选定的组合框值传递给名为service1.svc的wcf服务。我无法理解如何将所选项目作为参数传递给服务。你能帮帮我吗?感谢
我的服务参考如下
命名空间SilverlightApplication2.Web {
public class Service1
{
[OperationContract]
public ObservableCollection<Employee> GetAllEmployees(string brandID)
{
var emps = new ObservableCollection<Employee>();
string connect = ConfigurationManager.ConnectionStrings["yoyo"].ToString();
using(var con = new OdbcConnection(connect))
{
string query = "Select new,brand,imagelink FROM pivottable WHERE brand='"+brandID+"'";
var cmd = new OdbcCommand(query, con);
con.Open();
using (var dr = cmd.ExecuteReader())
{
while(dr.Read())
{
var emp = new Employee();
emp.EmployeeID = dr.GetInt32(0);
emp.FirstName = dr.GetString(1);
emp.ImageURI = new Uri(dr.GetString(2));
emps.Add(emp);
}
}
}
return emps;
}
}
}
我的mainpage.xaml.cs如下
命名空间SilverlightApplication2 {
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
object selectedItem = Combobox.SelectedItem;
var proxy = new Service1Client(selectedItem.ToString());
// var proxy = new Service1Client();
proxy.GetAllEmployeesCompleted += proxy_GetAllEmployeesCompleted;
proxy.GetAllEmployeesAsync();
var proxytwo = new Service1Client();
proxytwo.GetAllBrandsCompleted += proxytwo_GetAllBrandsCompleted;
proxytwo.GetAllBrandsAsync();
}
void proxytwo_GetAllBrandsCompleted(object sender, GetAllBrandsCompletedEventArgs f)
{
Combobox.ItemsSource = f.Result;
}
void proxy_GetAllEmployeesCompleted(object sender,GetAllEmployeesCompletedEventArgs e)
{
Pivot.ItemsSource = e.Result;
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
object selectedItem = Combobox.SelectedItem;
MessageBox.Show("Selected item is" +selectedItem.ToString());
}
}
}
我将Object引用未设置为对象的实例。 at var proxy = new Service1Client(selectedItem.ToString());
答案 0 :(得分:0)
var proxy = new Service1Client();
proxy.GetAllEmployeesCompleted += proxy_GetAllEmployeesCompleted;
proxy.GetAllEmployeesAsync(selectedItem.ToString());
如果在最后一行遇到编译器错误,则需要更新服务代理。右键单击服务引用,然后在IDE语言中选择最接近“更新”的内容。