Longlist没有正确创建 [英] Longlist is not creating properly
本文介绍了Longlist没有正确创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过WCF服务填充长列表。代码中没有错误,只是执行程序的结果如下: PhoneApp1.ServiceReference1.worker 而不是我想要显示的名称和其他数据。我的服务实现是:
I want to populate longlist via WCF service. There are no errors in the code, just the result of the executed program looks like: "PhoneApp1.ServiceReference1.worker" instead of the name and other data I want to display. My service implementation is:
public IEnumerable<worker> GetStuffList()
{
List<worker> stuffList = new List<worker>();
stuffList.Add(new worker("John", 23, true));
stuffList.Add(new worker("Nick", 22, true));
stuffList.Add(new worker("Gill", 23, false));
return stuffList;
}
private List<Group<worker>> GetStuffEnumerable()
{
IEnumerable<worker> stuffList = GetStuffList();
return GetItemGroups(stuffList, c => c.Age.ToString());
}
private static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc)
{
IEnumerable<Group<T>> groupList = from item in itemList
group item by getKeyFunc(item) into g
orderby g.Key
select new Group<T>(g.Key, g);
return groupList.ToList();
}
public class Group<T> : List<T>
{
public Group(string name, IEnumerable<T> items)
: base(items)
{
this.Title = name;
}
public string Title
{
get;
set;
}
}
我的服务界面是:
My service interface is:
[ServiceContract]
public interface IService1
{
[OperationContract]
IEnumerable<worker> GetStuffList();
}
[DataContract]
public class worker
{
public string Name;
public int Age;
public bool Sex;
public worker(string Name, int Age, bool Sex)
{
this.Age = Age;
this.Name = Name;
this.Sex = Sex;
}
}
我的WP页面代码是:
My WP page code is:
private void Button_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
proxy.GetStuffListCompleted += showList;
proxy.GetStuffListAsync();
}
private void showList(object sender, ServiceReference1.GetStuffListCompletedEventArgs e)
{
this.longlist.ItemsSource = e.Result;
}
我认为它必须是this.longlist.ItemsSource = e.Result,但不知道如何纠正它。
I think it has to be something with "this.longlist.ItemsSource = e.Result", but don't know how to correct it.
推荐答案
将DataMemberAttribute应用于必须序列化的字段(或属性)。
Apply the DataMemberAttribute to fields (or properties) that must be serialized.
[DataContract]
public class worker
{
[DataMember]
public string Name;
[DataMember]
public int Age;
[DataMember]
public bool Sex;
public worker(string Name, int Age, bool Sex)
{
this.Age = Age;
this.Name = Name;
this.Sex = Sex;
}
}
这篇关于Longlist没有正确创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文