簡體   English   中英

將數據綁定到xaml中的ListBox從Web服務獲取數據

[英]Binding data to ListBox in xaml getting data from webservice

我有以下代碼:

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。我必須綁定從Web服務獲取的數據,該數據通過在加載頁面時傳遞參數來調用稱為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個項目的數據。

我認為您遇到了這個問題,因為列表框使用“模型作為數據模板”,您從網絡服務器上獲取的是xml數據並轉為字符串,我之前做過類似的事情,並且還做了類似的事情……

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

如您所說,您將獲得603個結果,但它們無法在DataTemplate模型上識別

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM