2016-04-18 19 views
1

3つのタスクをコレクションに格納するだけでなく、コレクション内で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

私はWebから3回の呼び出しをしていると言いますが、それはAPIまたはXML/JSON要求ではないため、私は自分の記事でカテゴリを分類する必要があります。私は複数のタスクのHTTP要求を使用して、私は3つのURLの配列を持っています。私は1つのObservableCollectionにこれらの3つのURLのコンテンツを追加したいが、それでもそれらを必要とするカテゴリにすべての記事を区別することができる。 – 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」という名前の文字列リストにデータを配置していることがわかります。このリストを「Headers」クラスのデータソースとして使用し続けます。このように、「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のクラスです。私のサンプルで

、あなたのウリの形式は、常にこれらのパターンに従ってください:

君はGetItemsGrouped()メソッドのコードをHeadersクラスに変更して、期待どおりのパターンに合わせることができます。

これは、このサンプルの結果である:あなたは私のサンプルをテストしたい場合には

enter image description here

、ここit(Grouped List)です。

+0

ありがとう男。私は感謝します! – Cody

+0

caseオペレーションを使う代わりに、if(url.contains( "unique" eg New){set isNew true to true}を使用して、私が意味するものを得ます。新しいものが真であるマスターコレクションと、別のコレクションを「お気に入り」のコレクションにして、お気に入りが真であるマスターコレクションから選択します。 – Cody

+0

@Cody、それを行うことは賢明です。 –

関連する問題