如何复制列表从类到另一个类在Windows Phone 8.1与解析页面

本文关键字:Phone Windows 另一个 何复制 复制 列表 | 更新日期: 2023-09-27 17:51:20

我想从PurePcData中的list_data复制项目到MainPage -> list_PurePc。

MainPage.cs:

public List<PurePcData> list_PurePc = new List<PurePcData>();
public MainPage()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Required;
    PurePcData purePc = new PurePcData();
}

PurePcData.cs:

public class PurePcData
{
    private string url = "http://www....";
    public string Title { get; set; }
    public BitmapImage Image { get; set; }
    public string Content { get; set; }
    public List<PurePcData> list_data = new List<PurePcData>();
    public MainPage main;
    public PurePcData()
    {
        Parsing();
    }
    public PurePcData(string title, BitmapImage image, string content)
    {
        Title = title;
        Image = image;
        Content = content;
    }

    private async void Parsing()
    {
        try
        {
            string url_string;
            using (var client = new HttpClient())
            {
                url_string = await client.GetStringAsync(new Uri(url));
            }
            HtmlDocument htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(url_string);
            HtmlNode node = htmlDocument.DocumentNode.Descendants("div").FirstOrDefault(o => o.GetAttributeValue("class", null) == "latest_items");
            HtmlNodeCollection nodeCollection = node.ChildNodes;
            foreach (HtmlNode itemNode in nodeCollection)
            {
                var titleAndImage = itemNode.Descendants("a").FirstOrDefault(x => x.GetAttributeValue("class", null) == "ni_image");
                if (titleAndImage != null)
                {
                    var attributes = titleAndImage.Descendants("img").FirstOrDefault(x => x.GetAttributeValue("alt", "") != null);
                    var title = attributes.Attributes["alt"].Value;
                    var image = attributes.Attributes["src"].Value;
                    list_data.Add(new PurePcData(title, new BitmapImage(new Uri(image)), ""));
                }
            }
            //IN THIS PLACE, I want Send DATA FROM list_data to list_PurePc in MainPage
        }
        catch (Exception e)
        {
            MessageDialog msgDialog = new MessageDialog(e.Message);
            msgDialog.ShowAsync();
        }
    }

我尝试使用静态列表,对象来做到这一点。毫无效果。

请帮我解决这个问题。

如何复制列表从类到另一个类在Windows Phone 8.1与解析页面

我发现了问题,这是异步方法。当客户端下载html代码到"url_string"字段时,方法继续工作。有没有办法,该方法一直等到下载数据?还是将方法转换为非异步?