解析 xml 并将数据绑定到列表框

发布于 2024-11-24 05:59:13 字数 2631 浏览 1 评论 0原文

这是非常简单的任务,但我却奋斗了几个小时。 我已经从 Web 源解析 xml 并将它们绑定到列表框。现在我想为绑定到列表框的每个项目创建一个索引,如下所示:

1.Title   2.Title   3.Title
  Author    Author    Author
  Date      Date      Date

这是我到目前为止所拥有的:

                <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Name="stkPnl"  Orientation="Horizontal" Margin="15,0" MouseEnter="stkPnl_MouseEnter" MouseLeave="stkPnl_MouseLeave">
                        <Image x:Name="imageAV" Source="{Binding avlink}" Height="80" Width="80" 
                               Stretch="UniformToFill" MouseLeftButtonUp="imageAV_MouseLeftButtonUp" ImageFailed="imageAV_ImageFailed"/>
                        <StackPanel Orientation="Vertical" Margin="10,0,0,0" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp">                                                               
                            <TextBlock Text="{Binding nickname}" Width="Auto" />                               
                            <TextBlock Text="{Binding track}" FontWeight="Bold" Width="Auto"/>
                            <TextBlock Text="{Binding artist}" Width="Auto"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

和 MainPage.xaml.cs

        private void DoWebClient()
    {
        var webClient = new WebClient();
        webClient.OpenReadAsync(new Uri("http://music.mobion.vn/api/v1/music/userstop?devid="));
        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);

    }

    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        using (var reader = new StreamReader(e.Result))
        {

            string s = reader.ReadToEnd();
            Stream str = e.Result;
            str.Position = 0;
            XDocument xdoc = XDocument.Load(str);

            var data = from query in xdoc.Descendants("user")
                       select new mobion
                       {
                           avlink = (string)query.Element("user_info").Element("avlink"),
                           nickname = (string)query.Element("user_info").Element("nickname"),
                           track = (string)query.Element("track"),
                           artist = (string)query.Element("artist"),
                       };
            listBox.ItemsSource = data;
        }
    }

如您所见,如果我想添加,我只有昵称、曲目和艺术家为每个 listboxItem 增加的索引,我该怎么做? 感谢您阅读这个问题。

It's very simple task, but I struggle for hours.
I have parse xml from web sources and bind them to listbox. Now I want to make an index for each items binded to list box, something like this:

1.Title   2.Title   3.Title
  Author    Author    Author
  Date      Date      Date

Here is what I have so far:

                <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Name="stkPnl"  Orientation="Horizontal" Margin="15,0" MouseEnter="stkPnl_MouseEnter" MouseLeave="stkPnl_MouseLeave">
                        <Image x:Name="imageAV" Source="{Binding avlink}" Height="80" Width="80" 
                               Stretch="UniformToFill" MouseLeftButtonUp="imageAV_MouseLeftButtonUp" ImageFailed="imageAV_ImageFailed"/>
                        <StackPanel Orientation="Vertical" Margin="10,0,0,0" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp">                                                               
                            <TextBlock Text="{Binding nickname}" Width="Auto" />                               
                            <TextBlock Text="{Binding track}" FontWeight="Bold" Width="Auto"/>
                            <TextBlock Text="{Binding artist}" Width="Auto"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

and MainPage.xaml.cs

        private void DoWebClient()
    {
        var webClient = new WebClient();
        webClient.OpenReadAsync(new Uri("http://music.mobion.vn/api/v1/music/userstop?devid="));
        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);

    }

    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        using (var reader = new StreamReader(e.Result))
        {

            string s = reader.ReadToEnd();
            Stream str = e.Result;
            str.Position = 0;
            XDocument xdoc = XDocument.Load(str);

            var data = from query in xdoc.Descendants("user")
                       select new mobion
                       {
                           avlink = (string)query.Element("user_info").Element("avlink"),
                           nickname = (string)query.Element("user_info").Element("nickname"),
                           track = (string)query.Element("track"),
                           artist = (string)query.Element("artist"),
                       };
            listBox.ItemsSource = data;
        }
    }

As you see I only have nickname, track and artist, if I want to add an index that increase for each listboxItem, how can I do that?
Thank you for reading this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-12-01 05:59:13

我知道这很丑陋,但这是一个想法:为您的 mobion 类创建一个包装类:

public class mobionWrapper : mobion
{
    public int Index { get; set; }
}

您选择 mobionWrapper 实例而不是选择 mobion 实例:

var data = from query in xdoc.Descendants("user")
            select new mobionWrapper
            {
                avlink = (string)query.Element("user_info").Element("avlink"),
                nickname = (string)query.Element("user_info").Element("nickname"),
                track = (string)query.Element("track"),
                artist = (string)query.Element("artist"),
            };

绑定数据后,设置包装类的 Index 属性:

listBox.ItemsSource = data;

for(int i = 0; i < listBox.Items.Count; i++)
{
    var item = listBox.Items[i] as mobionWrapper;
    item.Index = i + 1;
}

现在您需要一个新的TextBlock 并将其绑定到 Index 属性:

<TextBlock Text="{Binding Index}" Width="Auto" />

对我有用。请记住,对数据显示进行排序或过滤后,索引可能无效。

I know it's ugly, but it's an idea: Create a wrapper class for around your mobion class:

public class mobionWrapper : mobion
{
    public int Index { get; set; }
}

Instead of selecting mobion instances you select mobionWrapper instances:

var data = from query in xdoc.Descendants("user")
            select new mobionWrapper
            {
                avlink = (string)query.Element("user_info").Element("avlink"),
                nickname = (string)query.Element("user_info").Element("nickname"),
                track = (string)query.Element("track"),
                artist = (string)query.Element("artist"),
            };

After binding the your data, set the Index property of your wrapper class:

listBox.ItemsSource = data;

for(int i = 0; i < listBox.Items.Count; i++)
{
    var item = listBox.Items[i] as mobionWrapper;
    item.Index = i + 1;
}

Now you need a new TextBlock and bind it to the Index property:

<TextBlock Text="{Binding Index}" Width="Auto" />

Worked for me. Keep in mind that the index might be invalid after sorting or filtering the data display.

淡淡的优雅 2024-12-01 05:59:13

假设您的 mobion 类中有一个适当的字段(您可以在视图中绑定到该字段),则可以在迭代文档时使用递增计数器来填充该字段。

int[] counter = { 1 };       

var data = from query in xdoc.Descendants("user")
           select new mobion
           {
               index = counter[0]++,
               avlink = (string)query.Element("user_info").Element("avlink"),
               nickname = (string)query.Element("user_info").Element("nickname"),
               track = (string)query.Element("track"),
               artist = (string)query.Element("artist"),
           };

请注意,counter 是一个 int 数组,而不仅仅是一个 int,以防止访问修改后的闭包。

Assuming you've an appropriate field in you mobion class (which you can bind to in the view), you can use an incrementing counter to populate this field as the document is iterated over.

int[] counter = { 1 };       

var data = from query in xdoc.Descendants("user")
           select new mobion
           {
               index = counter[0]++,
               avlink = (string)query.Element("user_info").Element("avlink"),
               nickname = (string)query.Element("user_info").Element("nickname"),
               track = (string)query.Element("track"),
               artist = (string)query.Element("artist"),
           };

Note that counter is an array of int rather than just an int to prevent accessing a modified closure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文