0

私は C# が初めてで、独学で C# を学んでいます。C# XAML で最初の win8 ストア アプリを作成しようとしています。アプリは私専用で、ストアには公開されません。アプリの Web はサイトをスクレイピングし、そこからいくつかのリンクとその説明を収集し、リストを埋めます。リストにはリンクと説明があり、次のようになります: リンク: www.google.com 説明: google

リンク: www.yahoo.com 説明: yahoo

私の最初の問題は、このデータを XAML ページに渡す方法がわからないことです。私のもう1つの問題は、ボタンの動的リストを作成する方法です。リストに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>

私の悪い英語でごめんなさい。

4

1 に答える 1

0

おそらく、リンクとその説明を保持する に名前を変更MyListする必要があります。LinkItem

  1. type のプロパティを追加してObservableCollection<LinkItem>入力します (この例では、 ill name it MyLinks)
  2. ListViewMainPage.xamlに a を追加します
  3. リスト ビューのItemsSourceプロパティを に設定し{Binding MyLinks}ます。ここまでで、各アイテムの「MyAppNamespace.LinkItem」文字列を表示するリストビューが作成されました。
  4. ButtonItemTemplateを表示し、そのContentプロパティを{Binding Description}
  5. Clickイベントのハンドラーを作成する

それがあなたを正しい道に送るのに十分であることを願っています。また、これらの概念のいくつかをよりよく理解するために、Windows 8 チュートリアルのいくつかを実行することから始める必要があります。

編集

あなたは私の指示に正しく従わなかった..これを試してください: MainPage にプロパティLinkItemを追加し、それにs を入力します:

public sealed partial class MainPage : Page
{
    ObservableCollection<LinkItem> LinkItems {get; set;} // <---------

    public MainPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        HtmlParser pars = new HtmlParser();
        pars.Uri = "http://some website.something/";
        LinksItems = await pars.Parse();    //<-------------
    }
}

パーサーが値を設定したコレクションを返すようにします。

public async Task<ObservableCollection<LinkItem>> Parse()
    {
        ObservableCollection<LinkItem> listDesc = new ObservableCollection<LinkItem>();
        //...
        return listDesc;
    }

バインディングをLinkItems(上で作成したコレクションの名前) に変更します。

<ListView x:Name="ListLinks" ItemsSource="{Binding LinkItems}"
      HorizontalAlignment="Left" Height="495" VerticalAlignment="Top" Width="382">
</ListView>
于 2013-09-05T12:42:55.167 に答える