....
<ListBox x:Name="lstBox"
Grid.Row="1"
FontSize="18"
BorderThickness="2,3,2,0"
DataContext="{Binding ElementName=lstBox}" FontStretch="ExtraExpanded" Background="#FF040924">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock x:Name="txtProductId"
Margin="15"
Text="{Binding Path=ProductId, Mode=OneWay}" Foreground="White"
FontWeight="ExtraBold"
FontSize="24"
FontFamily="Arial" />
<TextBlock x:Name="txtProductName"
Margin=" 15"
Text="{Binding ProductName, Mode=OneWay}" Foreground="White" FontWeight="ExtraBold"
FontSize="24"
FontFamily="Arial" />
<TextBlock x:Name="txtProductPrice"
Margin=" 15"
Text="{Binding ProductPrice, Mode=OneWay}" Foreground="White"
FontWeight="ExtraBold"
FontSize="24" FontFamily="Arial" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
Si è appena terminato di costruire una
form contenente un testo, un pulsante e una lista, ora va inserita un po' di logica. Per prima cosa aggiungete un riferimento al
WCF Service tramite
Add Service Reference nella finestra di progetto, e create la classe
OrderClient.cs nella quale inserite il seguente codice:
public class OrderClient
{ public string OrderNumber;
public string OrderId;
public string OrderCustomer;
}
quindi la logica vera e propria:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WindowsPhoneApplication1.WCFOrderService;
namespace WindowsPhoneApplicationOrderList
{ public partial class MainPage : PhoneApplicationPage
{ public MainPage()
{ InitializeComponent();
btnGetData.Click += new RoutedEventHandler(btnGetData_Click);
}
void btnGetData_Click(object sender, RoutedEventArgs e)
{ WCFOrderServiceClient proxy = new WCFOrderServiceClient();
proxy.GetOrderCompleted += new EventHandler<GetOrderCompletedEventArgs>(proxy_GetOrderCompleted);
proxy.GetOrderAsync();
}
void proxy_GetOrderCompleted(object sender, GetOrderCompletedEventArgs e)
{ List<OrderClient> lstResults = new List<OrderClient>();
OrderClient order;
List<OrderDTO> results = e.Result.ToList();
foreach (var r in results)
{ order = new OrderClient
{ OrderNumber = r.OrderNumber,
OrderId = r.OrderId,
OrderCustomer = r.OrderCustomer
};
lstResults.Add(order);
}
lstBox.ItemsSource = lstResults;
}
}
}
Alla pressione del pulsante si consuma il
WCF Service che restituisce in una
List di
OrderClient i dati riguardanti gli ordini e li passa alla
listbox presente nella form di Windows Phone 7.