将数据绑定到xaml中的ListBox,从webservice获取数据

时间:2015-12-24 14:42:11

标签: c# xaml windows-phone-8.1

我有这段代码:

Listbox

以下是包含GetFullClaimLinesAsync的XAML。我必须通过在加载页面时传递参数来绑定从webservice调用名为 <phone:PhoneApplicationPage x:Class="MEO.Views.Result" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" shell:SystemTray.IsVisible="True"> <!--LayoutRoot is the root grid where all page content is placed--> <Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Margin="12,17,0,28" Grid.ColumnSpan="2"> <Button Content="My Expenses On line" Background="#00ccff" Grid.Row="5" FontWeight="Bold" Name="head" Margin="-23,0,-13,-98" Foreground="White" BorderThickness="0"/> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Margin="0,35,24,10" Grid.ColumnSpan="2"/> <!--<Button Content="Get Full Claims" Background="#00ccff" FontWeight="Bold" x:Name="claim" Click="claim_Click" Margin="67,105,97,-203" Foreground="White" BorderThickness="0" Grid.Row="1" RenderTransformOrigin="0.676,0.469"/>--> <Grid Margin="0,203,10,-713" Grid.Row="1" > <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="0*"/> </Grid.ColumnDefinitions> <ListBox HorizontalAlignment="Left" Name="listbox1" ItemsSource="{Binding}" VerticalAlignment="Top" Height="500" Width="0"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"> </ColumnDefinition> <ColumnDefinition Width="50"> </ColumnDefinition> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Description}" Margin="=3" Grid.Column="0"></TextBlock> <TextBlock Text="{Binding ExpenseHeaderId}" Margin="=3" Grid.Column="1"></TextBlock> </Grid> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Grid> </phone:PhoneApplicationPage> 的方法的数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO;
using System.Net.NetworkInformation;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using MEO.MODELS;
namespace MEO.Views
{
    public partial class Result : PhoneApplicationPage
    {

        //public Guid rid = new Guid(NavigationContext.QueryString["id"]);
        //public string rpswd = NavigationContext.QueryString["password"];
        //public DateTime headerFromDate = DateTime.Today;
        //public DateTime detaiFromDate = DateTime.Today;        
        ObservableCollection<Claims> oc = new ObservableCollection<Claims>();

        public Result()
        {            
            InitializeComponent();            
            Guid rid = new Guid("0525466131154515");
            string rpswd = "hchdj455mjchdjkch7dc1njj";
            DateTime headerFromDate = Convert.ToDateTime("555525545");
            DateTime detaiFromDate = Convert.ToDateTime("38635");

            LoginService.DXDataMobileSoapClient client = new LoginService.DXDataMobileSoapClient();
            client.GetFullClaimLinesCompleted+=client_GetFullClaimLinesCompleted;
            client.GetFullClaimLinesAsync(rid, rpswd, "", headerFromDate, detaiFromDate, rid);
        }       

        private void client_GetFullClaimLinesCompleted(object sender, LoginService.GetFullClaimLinesCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    XElement[] array = Enumerable.ToArray<XElement>(e.Result.ReturnedDataTable.Any1.Descendants("ClaimHeadersDT"));
                    if (Enumerable.Count<XElement>(array) > 0)
                    {
                        //ObservableCollection<Claims> oc = new ObservableCollection<Claims>();
                        for (int i = 0; i < Enumerable.Count<XElement>(array); i++)
                        {
                            oc.Add(new Claims()
                            {
                                Description = (string)array[i].Element("h_description"),
                                ExpenseHeaderId =(string)array[i].Element("h_expense_headerID"),


                            });
                        }
                       listbox1.ItemsSource = oc;


                    }
                }
            }
            catch(Exception ex)
            {
                  throw ex;

            }
        }
    }
}

以下是代码隐藏文件(Result.xaml.cs):

ObservableCollection

我的问题是我从Web服务获取数据,即从XML获取数据,并且数据被添加到我的集合中,即App.xaml oc。但我无法将oc数据绑定到列表框。我在Listbox.Itemssource中收到错误,例如无法解决的异常。我的catch块中没有捕获错误。但是{{1}}包含数据,包含603项。

1 个答案:

答案 0 :(得分:0)

我认为你得到这个问题,因为列表框使用模型作为数据模板,你从网络服务器获得的是xml数据并转向字符串,我之前做了类似的事情,并且像那样goas ......

private void client_GetFullClaimLinesCompleted(class.......)
{
  oc.ItemsSource = e.Result;
}

正如您所说,您获得了603项结果但却无法在DataTemplate模型

上得到认可