在WPF和WCF之间传递参数

时间:2012-02-28 06:17:50

标签: wpf wcf list binding wcf-binding

我有一个带有Java客户端的自托管WCF应用程序。我有一个WPF应用程序,它可以实现类

namespace WcfServiceLibrary6
{
    class Products
    {
        public string ProductID { get; set; }
        public string SerialNumber { get; set; }
        public string RangeOfProducts { get; set; }
        public string ProductDesc { get; set; }
        public string UnitPrice { get; set; }
        public string Discontinued { get; set; }
        public string Image { get; set; }
        //  public Bitmap Bimage = new Bitmap(50, 50);

        public System.Collections.IDictionary AttributeDictionary = new Dictionary<string, string>();
    }

}

数据存储为列表(wpf应用程序(C#)访问数据库)

现在我想将此列表绑定到wcf中的数据协定,以便它可以将信息发送到java客户端。我该怎么做呢该应用程序的其余部分完美运行。我只需要将列表值(wpf:List productList = new List();)分配给数据协定的成员。

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.ServiceModel;
  using System.Text;
  using WpfApplication24;

 namespace WcfServiceLibrary6
 {
// NOTE: If you change the interface name "IService1" here, you must also update the      reference to "IService1" in App.config.
[ServiceContract]
public interface IService1
{
    [OperationContract]
    Product getChars(int barid);

}

// Use a data contract as illustrated in the sample below to add composite types to service operations

[DataContract]
public class Product
{
    private string m_ProductID;
    private string m_SerialNumber;
    private string m_RangeOfProducts;
    private string m_ProductDesc;
    private int m_UnitPrice;
    private string m_Discontinued;

    [DataMember]
    public string ProductID
    {
        get { return m_ProductID; }
        set { m_ProductID = value; }
    }

    [DataMember]
    public string SerialNumber
    {
        get { return m_SerialNumber; }
        set { m_SerialNumber = value; }
    }

    [DataMember]
    public string RangeOfProducts
    {
        get { return m_RangeOfProducts; }
        set { m_RangeOfProducts = value; }
    }

    [DataMember]
    public string ProductDesc
    {
        get { return m_ProductDesc; }
        set { m_ProductDesc = value; }
    }

    [DataMember]
    public int UnitPrice
    {
        get { return m_UnitPrice; }
        set { m_UnitPrice = value; }
    }

    [DataMember]
    public string Discontinued
    {
        get { return m_Discontinued; }
        set { m_Discontinued = value; }
    }
}

}      

2 个答案:

答案 0 :(得分:2)

对于需要托管wcf服务帮助的人,我发现这个网站非常有用: wcfguidanceforwpf.codeplex.com

答案 1 :(得分:0)

使用OnSerializing Attribute添加方法:

    [OnSerializing()]
    public void fun()
    {

    }

可以从列表中填充产品属性。<​​/ p>