2016-04-18 118 views
1

我想将3个任务存储在一个集合中,但也能够在集合中识别它们。即哪个“链接,图像,标题”属于最喜欢的,哪些属于新的,哪些属于特征,就像它在Url的列表上一样。如果你能向我展示代码,我将不胜感激。识别属性

这里是我的代码:

private List<string> urlList() 
    { 
     List<string> urls = new List<string> 
     { 
      "http:favorite.com, 
      "http://new.com", 
      "http://feature.com" 
     }; 
     return urls; 
    } 

async Task GetData() 
    { 
     HttpClient client = new HttpClient(); 
     client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36"); 
     List<string> taskurl = urlList(); 
     IEnumerable<Task<int>> downloadTaskQuery = 
      from url in taskurl select ProcessURL(url, client); 
     List<Task<int>> downloadTask = downloadTaskQuery.ToList(); 
     while (downloadTask.Count > 0) 
     { 
      Task<int> firstFinishTask = await Task.WhenAny(downloadTask); 
      downloadTask.Remove(firstFinishTask); 
      int lenght = await firstFinishTask; 
     } 
    } 

private async Task<int> ProcessURL(string url, HttpClient client) 
    { 
     HttpResponseMessage response = await client.GetAsync(url); 
     var urlContent = await response.Content.ReadAsStringAsync(); 
var article = new Observable<Article>(); 
      foreach (var div in htmlDocument.DocumentNode.Descendants().Where(i => i.Name == "div" && i.GetAttributeValue("class", "").StartsWith("foo"))) 
      { 
     return something; 
    }  
} 

}

+0

目前还不清楚你在问什么; 'Category'是'Article'类的一个属性? – levelonehuman

+0

可以说我从网上打三个电话,因为它不是一个API或XML/JSON请求我正在做...我需要按我自己的文章分类。我正在使用多个任务http请求,我有一个3个网址的数组。我想将这3个url的内容添加到一个ObservableCollection中,但仍然能够将所有文章区分为我想要的类别。 – Cody

+0

我认为你应该为'Article'类添加一个'Category'属性。然后,您可以将其分类到您创建每篇文章的位置。这对你想要做什么有意义吗?例如 – levelonehuman

回答

0

正如我们昨天讨论的,如果分组列表可以解决你的问题,例如,你可以这样做:

<Page.Resources> 
    <CollectionViewSource x:Name="listViewItems" IsSourceGrouped="True" /> 
    <DataTemplate x:Name="listViewItemTemplate"> 
     <TextBlock Text="{Binding BookAddress}" FontSize="20" /> 
    </DataTemplate> 
</Page.Resources> 

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <ListView x:Name="listView" ItemsSource="{x:Bind listViewItems.View}" ItemTemplate="{StaticResource listViewItemTemplate}"> 
     <ListView.GroupStyle> 
      <GroupStyle> 
       <GroupStyle.HeaderTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Key}" FontSize="25" Foreground="Red" /> 
        </DataTemplate> 
       </GroupStyle.HeaderTemplate> 
      </GroupStyle> 
     </ListView.GroupStyle> 
    </ListView> 
</Grid> 

后面的代码很清楚:

public MainPage() 
{ 
    this.InitializeComponent(); 
    listViewItems.Source = Headers.GetItemsGrouped(); 
} 

我从你的代码中发现你把数据放在一个名为“urls”的字符串List中,我将继续在我的“Headers”类中使用这个List作为数据源,我的“Headers”类也是如此:

public class Headers 
{ 
    public string HeaderTitle { get; set; } 

    public Headers() 
    { 
     HeaderTitle = string.Empty; 
    } 

    private static List<string> urls = new List<string> 
    { 
     "http://favorite.com", 
     "http://new.com", 
     "http://feature.com", 
     "http://favorite.book1.com", 
     "http://new.book2.com", 
     "http://feature.book3.com", 
     "http://favorite.book4.com", 
     "http://new.book5.com", 
    }; 

    public static ObservableCollection<BookList> GetCollection() 
    { 
     ObservableCollection<BookList> myBookList = new ObservableCollection<BookList>(); 
     foreach (var book in urls) 
     { 
      myBookList.Add(new BookList(book)); 
     } 
     return myBookList; 
    } 

    public static ObservableCollection<GroupInfoList> GetItemsGrouped() 
    { 
     ObservableCollection<GroupInfoList> groups = new ObservableCollection<GroupInfoList>(); 

     var query = from item in GetCollection() 
        group item by item.BookAddress[9] into g 
        orderby g.Key 
        select new { GroupName = g.Key, Items = g }; 

     foreach (var g in query) 
     { 
      GroupInfoList info = new GroupInfoList(); 

      switch (g.GroupName.ToString()) 
      { 
       case "v": 
        info.Key = "Favorite"; 
        break; 

       case "w": 
        info.Key = "New"; 
        break; 

       case "a": 
        info.Key = "Feature"; 
        break; 

       default: 
        info.Key = g.GroupName; 
        break; 
      } 

      foreach (var item in g.Items) 
      { 
       info.Add(item); 
      } 
      groups.Add(info); 
     } 
     return groups; 
    } 
} 

,也是我BookList类和GroupInfoList是这样的:

public class BookList : INotifyPropertyChanged 
{ 
    public string _BookAddress; 

    public string BookAddress 
    { 
     get { return _BookAddress; } 
     set 
     { 
      _BookAddress = value; 
      OnPropertyChanged("BookAddress"); 
     } 
    } 

    public BookList(string name) 
    { 
     this.BookAddress = name; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

public class GroupInfoList : List<object> 
{ 
    public object Key { get; set; } 
} 

BookList类是为ListView的项目,如果你想在每一个项目,以显示更多的细节,你可以添加属性这个班。而GroupInfoList类仅适用于每个组的Key

在我的示例,您的URI格式应始终遵循这些模式:

您可以修改Headers类的GetItemsGrouped()方法中的代码以满足您的预期模式。

这是该样品的结果:

enter image description here

在你想测试我的样品的情况下,这里是it(Grouped List)

+0

谢谢你。我欣赏! – Cody

+0

而不是使用大小写操作我使用if(url.contains(“唯一”,例如新){set isNew to true}我明白了,然后我可以创建一个“新”集合,然后使用linq查询从中进行选择主集合中新的是真的,我会做另一个“最喜欢的”集合,并从主集合中选择最喜欢的是真实的,你明白我的意思了 – Cody

+0

@Cody,这样做更聪明,很好知道;) –