将内容添加到Windows 8中的groupsitemspage

时间:2013-02-16 09:39:27

标签: c# windows-8 httpclient

当我创建GroupedItemsPage时,它有例如DataModel / SampleDataSource.cs

但我如何添加从Internet加载并使用Fizzler修改的内容?

 public sealed class SampleDataSource
  {
    string a = "";
    private static SampleDataSource _sampleDataSource = new SampleDataSource();

    private ObservableCollection<SampleDataGroup> _allGroups = new ObservableCollection<SampleDataGroup>();
    public ObservableCollection<SampleDataGroup> AllGroups
    {
        get { return this._allGroups; }
    }

    public static IEnumerable<SampleDataGroup> GetGroups(string uniqueId)
    {
        if (!uniqueId.Equals("AllGroups")) throw new ArgumentException("Only 'AllGroups' is supported as a collection of groups");

        return _sampleDataSource.AllGroups;
    }

    public static SampleDataGroup GetGroup(string uniqueId)
    {
        // Simple linear search is acceptable for small data sets
        var matches = _sampleDataSource.AllGroups.Where((group) => group.UniqueId.Equals(uniqueId));
        if (matches.Count() == 1) return matches.First();
        return null;
    }

    public static SampleDataItem GetItem(string uniqueId)
    {
        // Simple linear search is acceptable for small data sets
        var matches = _sampleDataSource.AllGroups.SelectMany(group => group.Items).Where((item) => item.UniqueId.Equals(uniqueId));
        if (matches.Count() == 1) return matches.First();
        return null;
    }

    public async Task<string> DownloadStringContent()
    {
        HttpClient client = new HttpClient();
        var response = await client.GetAsync(new Uri("http://www.google.com"));
        var result = await response.RequestMessage.Content.ReadAsStringAsync();

        return result;
    }

    public async void setA()
    {
        a = await DownloadStringContent();
        while (a == "") ;
    }

    public SampleDataSource()
    {
        setA();
        String ITEM_CONTENT = a;
        //String ITEM_CONTENT = String.Format("Item Content: {0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}\n\n{0}",
          //          "Curabitur class aliquam vestibulum nam curae maecenas sed integer cras phasellus suspendisse quisque donec dis praesent accumsan bibendum pellentesque condimentum adipiscing etiam consequat vivamus dictumst aliquam duis convallis scelerisque est parturient ullamcorper aliquet fusce suspendisse nunc hac eleifend amet blandit facilisi condimentum commodo scelerisque faucibus aenean ullamcorper ante mauris dignissim consectetuer nullam lorem vestibulum habitant conubia elementum pellentesque morbi facilisis arcu sollicitudin diam cubilia aptent vestibulum auctor eget dapibus pellentesque inceptos leo egestas interdum nulla consectetuer suspendisse adipiscing pellentesque proin lobortis sollicitudin augue elit mus congue fermentum parturient fringilla euismod feugiat");

        var group1 = new SampleDataGroup("Group-1",
                "Group Title: 1",
                "Group Subtitle: 1",
                "Assets/DarkGray.png",
                "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante");
        group1.Items.Add(new SampleDataItem("Group-1-Item-1",
                "Item Title: 1",
                "Item Subtitle: 1",
                "Assets/LightGray.png",
                "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                ITEM_CONTENT,
                group1));
        group1.Items.Add(new SampleDataItem("Group-1-Item-2",
                "Item Title: 2",
                "Item Subtitle: 2",
                "Assets/DarkGray.png",
                "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                ITEM_CONTENT,
                group1));
        group1.Items.Add(new SampleDataItem("Group-1-Item-3",
                "Item Title: 3",
                "Item Subtitle: 3",
                "Assets/MediumGray.png",
                "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                ITEM_CONTENT,
                group1));
        group1.Items.Add(new SampleDataItem("Group-1-Item-4",
                "Item Title: 4",
                "Item Subtitle: 4",
                "Assets/DarkGray.png",
                "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                ITEM_CONTENT,
                group1));
        group1.Items.Add(new SampleDataItem("Group-1-Item-5",
                "Item Title: 5",
                "Item Subtitle: 5",
                "Assets/MediumGray.png",
                "Item Description: Pellentesque porta, mauris quis interdum vehicula, urna sapien ultrices velit, nec venenatis dui odio in augue. Cras posuere, enim a cursus convallis, neque turpis malesuada erat, ut adipiscing neque tortor ac erat.",
                ITEM_CONTENT,
                group1));
        this.AllGroups.Add(group1);
    }
}

我想通过httpclient中的下载字符串修改SampleDataSource中的字符串,但

中没有任何内容

我怎么处理这个?请帮帮我

1 个答案:

答案 0 :(得分:0)

也许在MSDN文档中读取核心级别..

Web服务返回2模型方法:

  • 普通方法
  • 异步方法

如果要使用异步方法,请在方法中声明“async”子句。 如果调用web服务方法async并且添加“await”子句:方法等待web方法响应。在这个时候,不要到达代码行。

和constuctors不支持async子句。

将您的集合绑定一个控件。仍在下载以显示在控制中。派生你的班级INotifyProperty。

问候。