识别属性 [英] Identifying properties

查看:90
本文介绍了识别属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将3个任务存储在一个集合中,但也能够在集合中识别它们.即,哪些链接,图片,标题"属于收藏夹",哪些属于新",哪个属于功能",就像在网址"列表中一样.如果您能用代码给我看,我将不胜感激.

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.

这是我的代码:

 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>

后面的代码很清楚:

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

我从您的代码中发现,您将数据放入了一个名为"urls"的字符串列表中,我将继续在"Headers"类中将此列表用作数据源,而我的"Headers"类也是这样:

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;
    }
}

我的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.

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.

在我的示例中,您的Uri格式应始终遵循以下格式:

In my sample, your Uri's format should always follow these patterns:

" http://new.(balabla).com "

" http://feature.(balabla).com "

您可以在Headers类的GetItemsGrouped()方法中修改代码以符合您期望的模式.

You can modify the code in the GetItemsGrouped() method of Headers class to meet your expected pattern.

这是该示例的结果:

如果要测试我的示例,请参见 it(分组列表).

In case you want to test my sample, here is it(Grouped List).

这篇关于识别属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆