【问题标题】:WP8 - Display result of WebService in grid's columnsWP8 - 在网格的列中显示 WebService 的结果
【发布时间】:2013-10-23 07:55:44
【问题描述】:

您好,我已经构建了一个从 SQL 返回数据的WebService

    public void ListadoWebService()
    {
       // InitializeComponent();
        ServiceTours.ServiceToursClient cl = new ServiceTours.ServiceToursClient();
        cl.ListadoCompleted += new EventHandler<ListadoCompletedEventArgs>(Listado2);
        cl.ListadoAsync();
    }
    private void Listado2(object sender, ListadoCompletedEventArgs e)
    {
        listB.ItemsSource = e.Result;
    }

现在我尝试在gridcolumns 中显示数据。我认为它可以将binding 的数据作为textblock 用于特定列,但即使数据在e.Result 中返回,我也无法显示数据。

我尝试了以下操作:

<ListBox x:Name="listB">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

    <TextBlock Text="{Binding id}" Grid.Column="0" />
    <TextBlock Text="{Binding name}" Grid.Column="1" />

                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

但我仍然有黑屏。

有人能帮我解决这个问题吗?

【问题讨论】:

  • 我编辑了我的帖子,所以你会明白的,我会在一分钟内给你写一个更完整的答案。 :)
  • @mishan 谢谢你。我试着按照你和 MansingDodiya 的建议去做,但我遇到了问题: lst.Add(new test(Id, Name));收到此错误:PhoneApp1.test 不包含采用 2 个参数的构造函数。我不知道如何解决,请您帮忙?
  • 是的,问题是我的定义不包含构造函数,而他的包含。只需在测试类中插入public test(string id, string name) { Id=id; Name=name; }。 :)
  • 我将重写我的代码,以便准确地解决您的问题。我没有准确地写出来,有些名字只是我认为的,而不是他们真正的名字:)
  • @mishan e.Result 不只是提供像 SecondProperty 这样的任何确切名称。非常感谢您的帮助。

标签: c# sql web-services linq-to-sql windows-phone-8


【解决方案1】:

首先你创建一个 getter setter 方法,比如

 public class test
    {
    public string Id{get;set;}
    public string Name{get;set;}

    public test(string id, string name)

    {
      Id=id;
      Name=name;
    }
     public test()

    {


    }

    than add your take one generc like 

    List<Test> lst=new List<test>();

    private void Listado2(object sender, ListadoCompletedEventArgs e)
        {
          lst.add(new test(id,name));
           listB.itemsource=lst;

        }

<ListBox x:Name="listB">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

    <TextBlock Text="{Binding Id}" Grid.Column="0" />
    <TextBlock Text="{Binding Name}" Grid.Column="1" />

                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

也请访问下面的链接,以便您了解更多信息

HOw to bind data in windows phone

How can I data bind a list of strings to a ListBox in WP7

希望它对你有用.....

【讨论】:

  • 感谢您对帮助我的兴趣,但我不确定如何实现您的代码以使其正常工作。
  • lst.Add(new test(Id, Name)); 收到此错误:PhoneApp1.test 不包含采用 2 个参数的构造函数。请问问题出在哪里?
  • 您的 Item 类看起来如何?它有承包商吗? - 里面的public test(string id, string name) { Id=id; Name=name; } 方法? - 错误说:我知道名为 test 的类,但我没有构造函数定义来说明我应该如何处理两个字符串参数。
  • @mishan:是的,它有构造函数
  • @MansinhDodiya 我知道你的类有一个构造函数,我在问他是否在他的代码中添加了构造函数。 :)
【解决方案2】:

我在这里可能错了,但是 Listado2 结束后 e.Result 不就不再存在了吗?

我不是很专业,但我会从结果中复制这些东西,这样我可以保存更长时间。

当我阅读 MansinhDodiya 之前的回答时,他基本上是在告诉你同样的事情。

  1. 创建一个名为 Item 的类,其中包含两个属性(public string Id{get;set;} 是一个属性)

    有更多方法可以做到这一点,其中一种是转到代码隐藏(该页面的 .xaml.cs 并在那里创建类,另一种是在同一个命名空间中创建一个新类,第三个,设置新的命名空间并添加 using)。

    所以在页面的 .xaml.cs 中创建这样的类:

     class Item
     {
        public string Id {get;set;} //this is the first property i would later bind
        public string Name {get;set;} //this is the second property
    
        public Item(string id, string name)  // this is the contructor, every time
                                             // an instance of Item is created, this
                                             // method is called
        {
           Id = id;
           Name = name;
        }
     }
    
  2. 在可访问的地方列出这些Items

    在该类旁边,在页面类中,创建该列表的一个实例:

    List<Item> itemlist = new List<Item>();
    
  3. 将数据复制到List&lt;Item&gt;,然后将该列表设置为列表框的itemsSource

    Listado2 内部,将e.Result 中的内容复制到itemlist

    private void Listado2(object sender, ListadoCompletedEventArgs e)
    {
       ...copying from e.Result into itemList...
       listB.itemsource = itemList;
    }
    
  4. 然后将 xaml 绑定定义更改为属性的名称——在我的例子中:

    <ListBox x:Name="listB">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                ..omitted...
                   <TextBlock Text="{Binding Id}" Grid.Column="0" />
                   <TextBlock Text="{Binding Name}" Grid.Column="1" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    

【讨论】:

  • 非常感谢!但是当我在Listado2Id = e.Result.Id 中进行操作时,没有.Id.Name 的报价。我不确定如何解决这个问题。
  • 我重做了它,省略了复制部分,因为我不知道结果如何。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 2011-04-03
  • 1970-01-01
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多