【问题标题】:Binding data to ListBox in xaml getting data from webservice在xaml中将数据绑定到ListBox从webservice获取数据
【发布时间】:2015-12-24 14:42:11
【问题描述】:

我有这个代码:

using System;
using System.Collections.Generic;
using System.linq;
using System.Text;
using System.Threading.Tasks;
using System.Component-model;
namespace MEO.MODELS
{
    public class  Claims :INotifyPropertyChanged
    {
        public Claims()
        {
        }

        private string description;
        private string expenseHeaderId;
        private string assginedTo;
        private bool submitted;
        private bool approved;
        private bool authorised;
        private DateTime updatedDate;
        private DateTime createdDate;
        private DateTime claimDate;
        private DateTime lastModifiedDate;
        private string expenseFormType;

        public bool Approved
        {
            get
            {
                return this.approved;
            }
            set
            {
                if (value != this.approved)
                {
                    this.approved = value;
                    this.NotfiyProperty("Approved");

                }
            }
        }

        public string AssignedTo
        {
            get
            {
                return this.assginedTo;
            }
            set
            {
                if (value != this.assginedTo)
                {
                    this.assginedTo = value;
                    this.NotfiyProperty("AssignedTo");
                }
            }
        }

        public bool Authorised
        {
            get
            {
                return this.authorised;
            }
            set
            {
                if (value != authorised)
                {
                    this.authorised = value;
                    this.NotfiyProperty("Authorised");
                }
            }
        }

        public bool Submitted
        {
            get
            {
                return this.submitted;
            }
            set
            {
                if (value != submitted)
                {
                    this.submitted = value;
                    this.NotfiyProperty("Submitted");
                }
            }
        }

        public DateTime ClaimDate
        {
            get
            {
                return this.claimDate;
            }
            set
            {
                if (value != claimDate)
                {
                    this.claimDate = value;
                    this.NotfiyProperty("ClaimDate");
                }
            }
        }

        public DateTime CreatedDate
        {
            get
            {
                return this.createdDate;
            }
            set
            {
                if (value != createdDate)
                {
                    this.createdDate = value;
                    this.NotfiyProperty("CreatedDate");
                }
            }
        }

        public DateTime LastModifiedDate
        {
            get
            {
                return this.lastModifiedDate;
            }
            set
            {
                if (value != lastModifiedDate)
                {
                    this.lastModifiedDate = value;
                    this.NotfiyProperty("LastModifiedDate");
                }
            }
        }

        public DateTime UpdatedDate
        {
            get
            {
                return this.updatedDate;
            }
            set
            {
                if (value != updatedDate)
                {
                    this.updatedDate = value;
                    this.NotfiyProperty("UpdatedDate");
                }
            }
        }

        public string Description
        {
            get
            {
                return this.description;
            }
            set
            {
                if (value != this.description)
                {
                    this.description = value;
                    this.NotfiyProperty("Description");
                }
            }
        }

        public string ExpenseFormType
        {
            get
            {
                return this.expenseFormType;
            }
            set
            {
                if (value != this.expenseFormType)
                {
                    this.expenseFormType = value;
                    this.NotfiyProperty("ExpenseFormType");
                }
            }
        }

        public string ExpenseHeaderId
        {
            get
            {
                return this.expenseHeaderId;
            }
            set
            {
                if (value != this.expenseHeaderId)
                {
                    this.expenseHeaderId = value;
                    this.NotfiyProperty("ExpenseHeaderId");
                }
            }
        }


        private void NotfiyProperty(string propertyName)
        {
            PropertyChangedEventHandler propertyChangedEventHandler = this.PropertyChanged;
            if (propertyChangedEventHandler != null)
            {
                propertyChangedEventHandler.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }        
        public event PropertyChangedEventHandler PropertyChanged;

    }
}

以下是包含Listbox的XAML。我必须在页面加载时通过传递参数来绑定从webservice调用一个名为GetFullClaimLinesAsync的方法的数据。

    <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>

以下是文件背后的代码(Result.xaml.cs):

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;

            }
        }
    }
}

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

【问题讨论】:

    标签: c# xaml windows-phone-8.1


    【解决方案1】:

    我认为您遇到了这个问题,因为列表框使用模型作为数据模板,您从网络服务器获得的是 xml 数据并转换为字符串,我之前做过类似的事情,然后 goas...

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

    如您所说,您获得了 603 个结果,但在 DataTemplate 模型上无法识别它们

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 2021-10-25
      相关资源
      最近更新 更多