I want to store the 3 task in a collection but also able to identify them in the collection. i.e. which "link, image, title" belongs to favorite and which ones belongs to new and which one belongs to featured just as it is on the list of Url. if you can show me with code i will be more than grateful.
Here is my code:
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;
}
}
}
As we've discussed yesterday, if a grouped list can solve your problem, you could do it for example like this:
<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>
code behind is quite clear:
public MainPage()
{
this.InitializeComponent();
listViewItems.Source = Headers.GetItemsGrouped();
}
I find from your code that you put the data in a string List named "urls", I will continue to use this List as data source in my "Headers" class, and so is my "Headers" class like this:
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;
}
}
And also my BookList
class and GroupInfoList
are like this:
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; }
}
The BookList
class is for the item of ListView
, if you want to show more details in each item, you can add properties in this class. And the GroupInfoList
class is just for the Key
of each group.
In my sample, your Uri's format should always follow these patterns:
"http://favorite.(balabla).com"
"http://new.(balabla).com"
"http://feature.(balabla).com"
You can modify the code in the GetItemsGrouped()
method of Headers
class to meet your expected pattern.
This is the result of this sample:
In case you want to test my sample, here is it(Grouped List).