识别属性

时间:2016-04-18 21:05:44

标签: c# boolean win-universal-app observablecollection identity

我想将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;
    }    
}

}

1 个答案:

答案 0 :(得分: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”类中使用这个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格式应始终遵循以下模式:

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

这是此示例的结果:

enter image description here

如果你想测试我的样本,这里是it(Grouped List)