public ObservableCollection<Offer> ModelPersons
{
get
{
return _persons;
}
set
{
_persons = value;
RaisePropertyChanged(() => ModelPersons);
}
}
//如何从ObservableCollection<Person>
获取Task<ObservableCollection<Person>>
?现在它抱怨无法转换源类型Task<Observable<Person>> to Observable<Person>
private void GetPersonOrders()
{
ModelPersons = _service.GetPersonOrders();
}
服务类中的:
private ObservableCollection<Person> _modelPersons;
public async Task<ObservableCollection<Person>> GetPersonOrders()
{
var req =
WebRequest.Create(
"http....")
as HttpWebRequest;
if (req == null) return null;
req.KeepAlive = false;
req.Method = "GET";
req.ContentType = "text/xml; encoding='utf-8'";
WebResponse response = req.GetResponse();
Stream receiveStream = response.GetResponseStream();
Encoding encode = Encoding.GetEncoding("utf-8");
string message = null;
if (receiveStream != null)
using (var readStream = new StreamReader(receiveStream, encode))
{
var result = new char[256];
int count = await readStream.ReadAsync(result, 0, 256);
while (count > 0)
{
var value = new String(result, 0, count);
message += value;
count = await readStream.ReadAsync(result, 0, 256);
}
var stringReader = new StringReader(message);
_modelPersons= new ObservableCollection<Person>();
var xmlTextReader = new XmlTextReader(stringReader);
xmlTextReader.WhitespaceHandling = WhitespaceHandling.None;
Person person = null;
string element = "";
while (xmlTextReader.Read())
{
if (xmlTextReader.NodeType == XmlNodeType.Element)
{
element = xmlTextReader.Name;
}
else if (xmlTextReader.NodeType == XmlNodeType.Text)
{
switch (element)
{
case "name":
person = new Person { Name = xmlTextReader.Value };
_modelPersons.Add(person);
break;
}
}
}
}
return _modelPersons;
}
答案 0 :(得分:2)
我建议您使用NotifyTaskCompletion
type from my AsyncEx library。
然后您的代码将如下所示:
private INotifyCompletion<ObservableCollection<Offer>> _persons;
public INotifyCompletion<ObservableCollection<Offer>> ModelPersons
{
get { return _persons; }
set
{
_persons = value;
RaisePropertyChanged(() => ModelPersons);
}
}
private void GetPersonOrders()
{
ModelPersons = NotifyCompletion.Create(_service.GetPersonOrdersAsync());
}
您的数据绑定将从ModelPersons
更改为ModelPersons.Result
。
进一步推荐阅读:
async
/await
intro及其后续资源,特别是TAP。async
properties。答案 1 :(得分:1)
由于您的方法返回Task<ObservableCollection<Person>>
,您可以通过Result
属性获取任务的结果。
private void GetPersonOrders()
{
ModelPersons = _service.GetPersonOrders().Result;
}
请注意,访问Result
属性将阻止调用线程,直到任务完成,即直到您收到WebResponse
。
另一种方法是使用ContinueWith
将延续任务附加到方法返回的任务。 continuation将属性设置为从task返回的值。
private void GetPersonOrders()
{
_service.GetPersonOrders()
.ContinueWith(t => ModelPersons = t.Result);
}