我是 C# 新手,我自学 C#。我正在尝试在 C# XAML 中创建我的第一个 win8 商店应用程序。该应用程序仅供我使用,不会发布到商店。应用程序网络抓取一个站点,并从中收集一些链接及其描述并填写列表。该列表具有链接和描述,如下所示:链接: www.google.com 描述:google
链接:www.yahoo.com 描述:雅虎
我的第一个问题是我不明白如何将此数据传递给 XAML 页面。我的另一个问题是如何创建一个动态的按钮列表,所以如果我的列表有 10 个元素,我想要 XAML 页面上的 10 个按钮。如果我的列表有 5 个元素,我想要 XAML 页面上的 5 个按钮。并且每个按钮都必须将其内容设置为我列表中的描述。当我单击一个按钮时,我想传递属于描述的链接并打开另一个 XAML 页面,我可以在其中使用该链接并对其进行操作。
我的 MainPage.xaml.cs 看起来像这样:
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public ObservableCollection<MyApp.HtmlParser.LinkItem> LinkItems { get; set; }
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
HtmlParser pars = new HtmlParser();
pars.Uri = "http://some website.something/";
//LinksItems = await pars.Parse();
ObservableCollection<MyApp.HtmlParser.LinkItem> LinksItem = await pars.Parse();
ListLinks.DataContext = LinkItems;
}
}
}
我的 HtmlParser 类如下所示:
{
class HtmlParser
{
private string sUri;
public string Uri
{
get { return this.sUri; }
set { this.sUri = value; }
}
public class LinkItem
{
public string link { get; set; }
public string description { get; set; }
public LinkItem(string Link, string Description)
{
this.link = Link;
this.description = Description;
}
}
public HtmlParser()
{
this.sUri = string.Empty;
}
public async Task<ObservableCollection<LinkItem>> Parse()
{
ObservableCollection<LinkItem> listDesc = new ObservableCollection<LinkItem>();
// Initialize http client.
HttpClient httpClient = new HttpClient();
var message = new HttpRequestMessage(HttpMethod.Get, this.sUri);
message.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
var response = await httpClient.SendAsync(message);
var result = response.Content.ReadAsStringAsync().Result;
HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("option");
HtmlDocument document = new HtmlDocument();
document.LoadHtml(result);
//pars web page
//var options = document.DocumentNode.Descendants("option").Skip(1)
// .Select(n => new
// {
// Value = n.Attributes["value"].Value,
// Text = n.InnerText
// })
// .ToList();
//pars mobile web page
var options = document.DocumentNode.Descendants("a").Skip(1)
.Select(n => new
{
Value = n.Attributes["href"].Value,
Text = n.InnerText,
})
.ToList();
foreach (var e in options)
{
// Add results to list:
listDesc.Add(new LinkItem( "http://mobile.blitz-cinestar.hr/" + e.Value, e.Text));
}
return listDesc;
}
}
}
我的 XAML 看起来像这样
<Page
x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<ListView x:Name="ListLinks" ItemsSource="{Binding}"
HorizontalAlignment="Left" Height="495" VerticalAlignment="Top" Width="382">
</ListView>
对不起,我的英语不好。