我是C#的新手,我自学C#。我正在尝试在C#XAML中创建我的第一个win8商店应用程序。 该应用仅供我使用,不会发布到商店。 应用程序Web会将网站剪贴,并从中收集一些链接及其描述并填写列表。 清单有链接和描述,看起来像这样: 链接:www.google.com 描述:谷歌动态创建按钮列表并传递其中的数据?
链接:www.yahoo.com 描述:雅虎
我的第一个问题是,我不不了解如何将这些数据传递给XAML页面。 而我的另一个问题是如何创建一个动态的按钮列表,如果我的列表有10个元素,我想在XAML页面上有10个按钮。如果我的列表有5个元素,我想在XAML页面上使用5个按钮。 并且每个按钮都必须将其内容设置为我的列表中的说明。 当我点击一个按钮时,我想传递属于描述的链接,并打开另一个XAML页面,我可以使用链接并使用它进行操作。
我MainPage.xaml.cs中看起来LIK这样的:
{
/// <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>
对不起,我的英语不好。
thx,我会尝试它,并报告回来,当我哈取得了一些成果。 – user2718165
我编辑了我的应用程序,但仍然无法填充ListView。当我运行我的应用程序时,我只能看到黑屏。我在做一些错误的事情。欢迎任何建议。 我在第一篇文章中编辑了代码,并在帖子中添加了Xaml代码。 – user2718165
我编辑了我的答案,试试。 – dcastro